Merge "Revert "Remove jquery.arrowSteps module""
[lhc/web/wiklou.git] / includes / db / Database.php
1 <?php
2 /**
3 * @defgroup Database Database
4 *
5 * This file deals with database interface functions
6 * and query specifics/optimisations.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Database
25 */
26 use Psr\Log\LoggerAwareInterface;
27 use Psr\Log\LoggerInterface;
28
29 /**
30 * Database abstraction object
31 * @ingroup Database
32 */
33 abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
34 /** Number of times to re-try an operation in case of deadlock */
35 const DEADLOCK_TRIES = 4;
36 /** Minimum time to wait before retry, in microseconds */
37 const DEADLOCK_DELAY_MIN = 500000;
38 /** Maximum time to wait before retry */
39 const DEADLOCK_DELAY_MAX = 1500000;
40
41 /** How long before it is worth doing a dummy query to test the connection */
42 const PING_TTL = 1.0;
43 const PING_QUERY = 'SELECT 1 AS ping';
44
45 const TINY_WRITE_SEC = .010;
46 const SLOW_WRITE_SEC = .500;
47 const SMALL_WRITE_ROWS = 100;
48
49 /** @var string SQL query */
50 protected $mLastQuery = '';
51 /** @var bool */
52 protected $mDoneWrites = false;
53 /** @var string|bool */
54 protected $mPHPError = false;
55 /** @var string */
56 protected $mServer;
57 /** @var string */
58 protected $mUser;
59 /** @var string */
60 protected $mPassword;
61 /** @var string */
62 protected $mDBname;
63 /** @var bool */
64 protected $cliMode;
65
66 /** @var BagOStuff APC cache */
67 protected $srvCache;
68 /** @var LoggerInterface */
69 protected $connLogger;
70 /** @var LoggerInterface */
71 protected $queryLogger;
72 /** @var callback Error logging callback */
73 protected $errorLogger;
74
75 /** @var resource Database connection */
76 protected $mConn = null;
77 /** @var bool */
78 protected $mOpened = false;
79
80 /** @var array[] List of (callable, method name) */
81 protected $mTrxIdleCallbacks = [];
82 /** @var array[] List of (callable, method name) */
83 protected $mTrxPreCommitCallbacks = [];
84 /** @var array[] List of (callable, method name) */
85 protected $mTrxEndCallbacks = [];
86 /** @var array[] Map of (name => (callable, method name)) */
87 protected $mTrxRecurringCallbacks = [];
88 /** @var bool Whether to suppress triggering of transaction end callbacks */
89 protected $mTrxEndCallbacksSuppressed = false;
90
91 /** @var string */
92 protected $mTablePrefix;
93 /** @var string */
94 protected $mSchema;
95 /** @var integer */
96 protected $mFlags;
97 /** @var bool */
98 protected $mForeign;
99 /** @var array */
100 protected $mLBInfo = [];
101 /** @var bool|null */
102 protected $mDefaultBigSelects = null;
103 /** @var array|bool */
104 protected $mSchemaVars = false;
105 /** @var array */
106 protected $mSessionVars = [];
107 /** @var array|null */
108 protected $preparedArgs;
109 /** @var string|bool|null Stashed value of html_errors INI setting */
110 protected $htmlErrors;
111 /** @var string */
112 protected $delimiter = ';';
113
114 /**
115 * Either 1 if a transaction is active or 0 otherwise.
116 * The other Trx fields may not be meaningfull if this is 0.
117 *
118 * @var int
119 */
120 protected $mTrxLevel = 0;
121 /**
122 * Either a short hexidecimal string if a transaction is active or ""
123 *
124 * @var string
125 * @see DatabaseBase::mTrxLevel
126 */
127 protected $mTrxShortId = '';
128 /**
129 * The UNIX time that the transaction started. Callers can assume that if
130 * snapshot isolation is used, then the data is *at least* up to date to that
131 * point (possibly more up-to-date since the first SELECT defines the snapshot).
132 *
133 * @var float|null
134 * @see DatabaseBase::mTrxLevel
135 */
136 private $mTrxTimestamp = null;
137 /** @var float Lag estimate at the time of BEGIN */
138 private $mTrxReplicaLag = null;
139 /**
140 * Remembers the function name given for starting the most recent transaction via begin().
141 * Used to provide additional context for error reporting.
142 *
143 * @var string
144 * @see DatabaseBase::mTrxLevel
145 */
146 private $mTrxFname = null;
147 /**
148 * Record if possible write queries were done in the last transaction started
149 *
150 * @var bool
151 * @see DatabaseBase::mTrxLevel
152 */
153 private $mTrxDoneWrites = false;
154 /**
155 * Record if the current transaction was started implicitly due to DBO_TRX being set.
156 *
157 * @var bool
158 * @see DatabaseBase::mTrxLevel
159 */
160 private $mTrxAutomatic = false;
161 /**
162 * Array of levels of atomicity within transactions
163 *
164 * @var array
165 */
166 private $mTrxAtomicLevels = [];
167 /**
168 * Record if the current transaction was started implicitly by DatabaseBase::startAtomic
169 *
170 * @var bool
171 */
172 private $mTrxAutomaticAtomic = false;
173 /**
174 * Track the write query callers of the current transaction
175 *
176 * @var string[]
177 */
178 private $mTrxWriteCallers = [];
179 /**
180 * @var float Seconds spent in write queries for the current transaction
181 */
182 private $mTrxWriteDuration = 0.0;
183 /**
184 * @var integer Number of write queries for the current transaction
185 */
186 private $mTrxWriteQueryCount = 0;
187 /**
188 * @var float Like mTrxWriteQueryCount but excludes lock-bound, easy to replicate, queries
189 */
190 private $mTrxWriteAdjDuration = 0.0;
191 /**
192 * @var integer Number of write queries counted in mTrxWriteAdjDuration
193 */
194 private $mTrxWriteAdjQueryCount = 0;
195 /**
196 * @var float RTT time estimate
197 */
198 private $mRTTEstimate = 0.0;
199
200 /** @var array Map of (name => 1) for locks obtained via lock() */
201 private $mNamedLocksHeld = [];
202
203 /** @var IDatabase|null Lazy handle to the master DB this server replicates from */
204 private $lazyMasterHandle;
205
206 /**
207 * @since 1.21
208 * @var resource File handle for upgrade
209 */
210 protected $fileHandle = null;
211
212 /**
213 * @since 1.22
214 * @var string[] Process cache of VIEWs names in the database
215 */
216 protected $allViews = null;
217
218 /** @var float UNIX timestamp */
219 protected $lastPing = 0.0;
220
221 /** @var int[] Prior mFlags values */
222 private $priorFlags = [];
223
224 /** @var Profiler */
225 protected $profiler;
226 /** @var TransactionProfiler */
227 protected $trxProfiler;
228
229 /**
230 * Constructor.
231 *
232 * FIXME: It is possible to construct a Database object with no associated
233 * connection object, by specifying no parameters to __construct(). This
234 * feature is deprecated and should be removed.
235 *
236 * IDatabase classes should not be constructed directly in external
237 * code. DatabaseBase::factory() should be used instead.
238 *
239 * @param array $params Parameters passed from DatabaseBase::factory()
240 */
241 function __construct( array $params ) {
242 global $wgDBprefix, $wgDBmwschema;
243
244 $this->srvCache = ObjectCache::getLocalServerInstance( 'hash' );
245
246 $server = $params['host'];
247 $user = $params['user'];
248 $password = $params['password'];
249 $dbName = $params['dbname'];
250 $flags = $params['flags'];
251 $tablePrefix = $params['tablePrefix'];
252 $schema = $params['schema'];
253 $foreign = $params['foreign'];
254
255 $this->cliMode = isset( $params['cliMode'] )
256 ? $params['cliMode']
257 : ( PHP_SAPI === 'cli' );
258
259 $this->mFlags = $flags;
260 if ( $this->mFlags & DBO_DEFAULT ) {
261 if ( $this->cliMode ) {
262 $this->mFlags &= ~DBO_TRX;
263 } else {
264 $this->mFlags |= DBO_TRX;
265 }
266 }
267
268 $this->mSessionVars = $params['variables'];
269
270 /** Get the default table prefix*/
271 if ( $tablePrefix === 'get from global' ) {
272 $this->mTablePrefix = $wgDBprefix;
273 } else {
274 $this->mTablePrefix = $tablePrefix;
275 }
276
277 /** Get the database schema*/
278 if ( $schema === 'get from global' ) {
279 $this->mSchema = $wgDBmwschema;
280 } else {
281 $this->mSchema = $schema;
282 }
283
284 $this->mForeign = $foreign;
285
286 $this->profiler = isset( $params['profiler'] )
287 ? $params['profiler']
288 : Profiler::instance(); // @TODO: remove global state
289 $this->trxProfiler = isset( $params['trxProfiler'] )
290 ? $params['trxProfiler']
291 : new TransactionProfiler();
292 $this->connLogger = isset( $params['connLogger'] )
293 ? $params['connLogger']
294 : new \Psr\Log\NullLogger();
295 $this->queryLogger = isset( $params['queryLogger'] )
296 ? $params['queryLogger']
297 : new \Psr\Log\NullLogger();
298
299 if ( $user ) {
300 $this->open( $server, $user, $password, $dbName );
301 }
302 }
303
304 /**
305 * Given a DB type, construct the name of the appropriate child class of
306 * IDatabase. This is designed to replace all of the manual stuff like:
307 * $class = 'Database' . ucfirst( strtolower( $dbType ) );
308 * as well as validate against the canonical list of DB types we have
309 *
310 * This factory function is mostly useful for when you need to connect to a
311 * database other than the MediaWiki default (such as for external auth,
312 * an extension, et cetera). Do not use this to connect to the MediaWiki
313 * database. Example uses in core:
314 * @see LoadBalancer::reallyOpenConnection()
315 * @see ForeignDBRepo::getMasterDB()
316 * @see WebInstallerDBConnect::execute()
317 *
318 * @since 1.18
319 *
320 * @param string $dbType A possible DB type
321 * @param array $p An array of options to pass to the constructor.
322 * Valid options are: host, user, password, dbname, flags, tablePrefix, schema, driver
323 * @return IDatabase|null If the database driver or extension cannot be found
324 * @throws InvalidArgumentException If the database driver or extension cannot be found
325 */
326 final public static function factory( $dbType, $p = [] ) {
327 global $wgCommandLineMode;
328
329 $canonicalDBTypes = [
330 'mysql' => [ 'mysqli', 'mysql' ],
331 'postgres' => [],
332 'sqlite' => [],
333 'oracle' => [],
334 'mssql' => [],
335 ];
336
337 $driver = false;
338 $dbType = strtolower( $dbType );
339 if ( isset( $canonicalDBTypes[$dbType] ) && $canonicalDBTypes[$dbType] ) {
340 $possibleDrivers = $canonicalDBTypes[$dbType];
341 if ( !empty( $p['driver'] ) ) {
342 if ( in_array( $p['driver'], $possibleDrivers ) ) {
343 $driver = $p['driver'];
344 } else {
345 throw new InvalidArgumentException( __METHOD__ .
346 " type '$dbType' does not support driver '{$p['driver']}'" );
347 }
348 } else {
349 foreach ( $possibleDrivers as $posDriver ) {
350 if ( extension_loaded( $posDriver ) ) {
351 $driver = $posDriver;
352 break;
353 }
354 }
355 }
356 } else {
357 $driver = $dbType;
358 }
359 if ( $driver === false ) {
360 throw new InvalidArgumentException( __METHOD__ .
361 " no viable database extension found for type '$dbType'" );
362 }
363
364 // Determine schema defaults. Currently Microsoft SQL Server uses $wgDBmwschema,
365 // and everything else doesn't use a schema (e.g. null)
366 // Although postgres and oracle support schemas, we don't use them (yet)
367 // to maintain backwards compatibility
368 $defaultSchemas = [
369 'mssql' => 'get from global',
370 ];
371
372 $class = 'Database' . ucfirst( $driver );
373 if ( class_exists( $class ) && is_subclass_of( $class, 'IDatabase' ) ) {
374 // Resolve some defaults for b/c
375 $p['host'] = isset( $p['host'] ) ? $p['host'] : false;
376 $p['user'] = isset( $p['user'] ) ? $p['user'] : false;
377 $p['password'] = isset( $p['password'] ) ? $p['password'] : false;
378 $p['dbname'] = isset( $p['dbname'] ) ? $p['dbname'] : false;
379 $p['flags'] = isset( $p['flags'] ) ? $p['flags'] : 0;
380 $p['variables'] = isset( $p['variables'] ) ? $p['variables'] : [];
381 $p['tablePrefix'] = isset( $p['tablePrefix'] ) ? $p['tablePrefix'] : 'get from global';
382 if ( !isset( $p['schema'] ) ) {
383 $p['schema'] = isset( $defaultSchemas[$dbType] ) ? $defaultSchemas[$dbType] : null;
384 }
385 $p['foreign'] = isset( $p['foreign'] ) ? $p['foreign'] : false;
386 $p['cliMode'] = $wgCommandLineMode;
387
388 $conn = new $class( $p );
389 if ( isset( $p['connLogger'] ) ) {
390 $conn->connLogger = $p['connLogger'];
391 }
392 if ( isset( $p['queryLogger'] ) ) {
393 $conn->queryLogger = $p['queryLogger'];
394 }
395 if ( isset( $p['errorLogger'] ) ) {
396 $conn->errorLogger = $p['errorLogger'];
397 } else {
398 $conn->errorLogger = [ MWExceptionHandler::class, 'logException' ];
399 }
400 } else {
401 $conn = null;
402 }
403
404 return $conn;
405 }
406
407 public function setLogger( LoggerInterface $logger ) {
408 $this->queryLogger = $logger;
409 }
410
411 public function getServerInfo() {
412 return $this->getServerVersion();
413 }
414
415 /**
416 * @return string Command delimiter used by this database engine
417 */
418 public function getDelimiter() {
419 return $this->delimiter;
420 }
421
422 /**
423 * Boolean, controls output of large amounts of debug information.
424 * @param bool|null $debug
425 * - true to enable debugging
426 * - false to disable debugging
427 * - omitted or null to do nothing
428 *
429 * @return bool|null Previous value of the flag
430 */
431 public function debug( $debug = null ) {
432 return wfSetBit( $this->mFlags, DBO_DEBUG, $debug );
433 }
434
435 public function bufferResults( $buffer = null ) {
436 if ( is_null( $buffer ) ) {
437 return !(bool)( $this->mFlags & DBO_NOBUFFER );
438 } else {
439 return !wfSetBit( $this->mFlags, DBO_NOBUFFER, !$buffer );
440 }
441 }
442
443 /**
444 * Turns on (false) or off (true) the automatic generation and sending
445 * of a "we're sorry, but there has been a database error" page on
446 * database errors. Default is on (false). When turned off, the
447 * code should use lastErrno() and lastError() to handle the
448 * situation as appropriate.
449 *
450 * Do not use this function outside of the Database classes.
451 *
452 * @param null|bool $ignoreErrors
453 * @return bool The previous value of the flag.
454 */
455 protected function ignoreErrors( $ignoreErrors = null ) {
456 return wfSetBit( $this->mFlags, DBO_IGNORE, $ignoreErrors );
457 }
458
459 public function trxLevel() {
460 return $this->mTrxLevel;
461 }
462
463 public function trxTimestamp() {
464 return $this->mTrxLevel ? $this->mTrxTimestamp : null;
465 }
466
467 public function tablePrefix( $prefix = null ) {
468 return wfSetVar( $this->mTablePrefix, $prefix );
469 }
470
471 public function dbSchema( $schema = null ) {
472 return wfSetVar( $this->mSchema, $schema );
473 }
474
475 /**
476 * Set the filehandle to copy write statements to.
477 *
478 * @param resource $fh File handle
479 */
480 public function setFileHandle( $fh ) {
481 $this->fileHandle = $fh;
482 }
483
484 public function getLBInfo( $name = null ) {
485 if ( is_null( $name ) ) {
486 return $this->mLBInfo;
487 } else {
488 if ( array_key_exists( $name, $this->mLBInfo ) ) {
489 return $this->mLBInfo[$name];
490 } else {
491 return null;
492 }
493 }
494 }
495
496 public function setLBInfo( $name, $value = null ) {
497 if ( is_null( $value ) ) {
498 $this->mLBInfo = $name;
499 } else {
500 $this->mLBInfo[$name] = $value;
501 }
502 }
503
504 public function setLazyMasterHandle( IDatabase $conn ) {
505 $this->lazyMasterHandle = $conn;
506 }
507
508 /**
509 * @return IDatabase|null
510 * @see setLazyMasterHandle()
511 * @since 1.27
512 */
513 public function getLazyMasterHandle() {
514 return $this->lazyMasterHandle;
515 }
516
517 /**
518 * @param TransactionProfiler $profiler
519 * @since 1.27
520 */
521 public function setTransactionProfiler( TransactionProfiler $profiler ) {
522 $this->trxProfiler = $profiler;
523 }
524
525 /**
526 * Returns true if this database supports (and uses) cascading deletes
527 *
528 * @return bool
529 */
530 public function cascadingDeletes() {
531 return false;
532 }
533
534 /**
535 * Returns true if this database supports (and uses) triggers (e.g. on the page table)
536 *
537 * @return bool
538 */
539 public function cleanupTriggers() {
540 return false;
541 }
542
543 /**
544 * Returns true if this database is strict about what can be put into an IP field.
545 * Specifically, it uses a NULL value instead of an empty string.
546 *
547 * @return bool
548 */
549 public function strictIPs() {
550 return false;
551 }
552
553 /**
554 * Returns true if this database uses timestamps rather than integers
555 *
556 * @return bool
557 */
558 public function realTimestamps() {
559 return false;
560 }
561
562 public function implicitGroupby() {
563 return true;
564 }
565
566 public function implicitOrderby() {
567 return true;
568 }
569
570 /**
571 * Returns true if this database can do a native search on IP columns
572 * e.g. this works as expected: .. WHERE rc_ip = '127.42.12.102/32';
573 *
574 * @return bool
575 */
576 public function searchableIPs() {
577 return false;
578 }
579
580 /**
581 * Returns true if this database can use functional indexes
582 *
583 * @return bool
584 */
585 public function functionalIndexes() {
586 return false;
587 }
588
589 public function lastQuery() {
590 return $this->mLastQuery;
591 }
592
593 public function doneWrites() {
594 return (bool)$this->mDoneWrites;
595 }
596
597 public function lastDoneWrites() {
598 return $this->mDoneWrites ?: false;
599 }
600
601 public function writesPending() {
602 return $this->mTrxLevel && $this->mTrxDoneWrites;
603 }
604
605 public function writesOrCallbacksPending() {
606 return $this->mTrxLevel && (
607 $this->mTrxDoneWrites || $this->mTrxIdleCallbacks || $this->mTrxPreCommitCallbacks
608 );
609 }
610
611 public function pendingWriteQueryDuration( $type = self::ESTIMATE_TOTAL ) {
612 if ( !$this->mTrxLevel ) {
613 return false;
614 } elseif ( !$this->mTrxDoneWrites ) {
615 return 0.0;
616 }
617
618 switch ( $type ) {
619 case self::ESTIMATE_DB_APPLY:
620 $this->ping( $rtt );
621 $rttAdjTotal = $this->mTrxWriteAdjQueryCount * $rtt;
622 $applyTime = max( $this->mTrxWriteAdjDuration - $rttAdjTotal, 0 );
623 // For omitted queries, make them count as something at least
624 $omitted = $this->mTrxWriteQueryCount - $this->mTrxWriteAdjQueryCount;
625 $applyTime += self::TINY_WRITE_SEC * $omitted;
626
627 return $applyTime;
628 default: // everything
629 return $this->mTrxWriteDuration;
630 }
631 }
632
633 public function pendingWriteCallers() {
634 return $this->mTrxLevel ? $this->mTrxWriteCallers : [];
635 }
636
637 public function isOpen() {
638 return $this->mOpened;
639 }
640
641 public function setFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
642 if ( $remember === self::REMEMBER_PRIOR ) {
643 array_push( $this->priorFlags, $this->mFlags );
644 }
645 $this->mFlags |= $flag;
646 }
647
648 public function clearFlag( $flag, $remember = self::REMEMBER_NOTHING ) {
649 if ( $remember === self::REMEMBER_PRIOR ) {
650 array_push( $this->priorFlags, $this->mFlags );
651 }
652 $this->mFlags &= ~$flag;
653 }
654
655 public function restoreFlags( $state = self::RESTORE_PRIOR ) {
656 if ( !$this->priorFlags ) {
657 return;
658 }
659
660 if ( $state === self::RESTORE_INITIAL ) {
661 $this->mFlags = reset( $this->priorFlags );
662 $this->priorFlags = [];
663 } else {
664 $this->mFlags = array_pop( $this->priorFlags );
665 }
666 }
667
668 public function getFlag( $flag ) {
669 return !!( $this->mFlags & $flag );
670 }
671
672 public function getProperty( $name ) {
673 return $this->$name;
674 }
675
676 public function getWikiID() {
677 if ( $this->mTablePrefix ) {
678 return "{$this->mDBname}-{$this->mTablePrefix}";
679 } else {
680 return $this->mDBname;
681 }
682 }
683
684 /**
685 * Return a path to the DBMS-specific SQL file if it exists,
686 * otherwise default SQL file
687 *
688 * @param string $filename
689 * @return string
690 */
691 private function getSqlFilePath( $filename ) {
692 global $IP;
693 $dbmsSpecificFilePath = "$IP/maintenance/" . $this->getType() . "/$filename";
694 if ( file_exists( $dbmsSpecificFilePath ) ) {
695 return $dbmsSpecificFilePath;
696 } else {
697 return "$IP/maintenance/$filename";
698 }
699 }
700
701 /**
702 * Return a path to the DBMS-specific schema file,
703 * otherwise default to tables.sql
704 *
705 * @return string
706 */
707 public function getSchemaPath() {
708 return $this->getSqlFilePath( 'tables.sql' );
709 }
710
711 /**
712 * Return a path to the DBMS-specific update key file,
713 * otherwise default to update-keys.sql
714 *
715 * @return string
716 */
717 public function getUpdateKeysPath() {
718 return $this->getSqlFilePath( 'update-keys.sql' );
719 }
720
721 /**
722 * Get information about an index into an object
723 * @param string $table Table name
724 * @param string $index Index name
725 * @param string $fname Calling function name
726 * @return mixed Database-specific index description class or false if the index does not exist
727 */
728 abstract function indexInfo( $table, $index, $fname = __METHOD__ );
729
730 /**
731 * Wrapper for addslashes()
732 *
733 * @param string $s String to be slashed.
734 * @return string Slashed string.
735 */
736 abstract function strencode( $s );
737
738 /**
739 * Called by serialize. Throw an exception when DB connection is serialized.
740 * This causes problems on some database engines because the connection is
741 * not restored on unserialize.
742 */
743 public function __sleep() {
744 throw new RuntimeException( 'Database serialization may cause problems, since ' .
745 'the connection is not restored on wakeup.' );
746 }
747
748 protected function installErrorHandler() {
749 $this->mPHPError = false;
750 $this->htmlErrors = ini_set( 'html_errors', '0' );
751 set_error_handler( [ $this, 'connectionerrorLogger' ] );
752 }
753
754 /**
755 * @return bool|string
756 */
757 protected function restoreErrorHandler() {
758 restore_error_handler();
759 if ( $this->htmlErrors !== false ) {
760 ini_set( 'html_errors', $this->htmlErrors );
761 }
762 if ( $this->mPHPError ) {
763 $error = preg_replace( '!\[<a.*</a>\]!', '', $this->mPHPError );
764 $error = preg_replace( '!^.*?:\s?(.*)$!', '$1', $error );
765
766 return $error;
767 } else {
768 return false;
769 }
770 }
771
772 /**
773 * @param int $errno
774 * @param string $errstr
775 */
776 public function connectionerrorLogger( $errno, $errstr ) {
777 $this->mPHPError = $errstr;
778 }
779
780 /**
781 * Create a log context to pass to PSR logging functions.
782 *
783 * @param array $extras Additional data to add to context
784 * @return array
785 */
786 protected function getLogContext( array $extras = [] ) {
787 return array_merge(
788 [
789 'db_server' => $this->mServer,
790 'db_name' => $this->mDBname,
791 'db_user' => $this->mUser,
792 ],
793 $extras
794 );
795 }
796
797 public function close() {
798 if ( $this->mConn ) {
799 if ( $this->trxLevel() ) {
800 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
801 }
802
803 $closed = $this->closeConnection();
804 $this->mConn = false;
805 } elseif ( $this->mTrxIdleCallbacks || $this->mTrxEndCallbacks ) { // sanity
806 throw new RuntimeException( "Transaction callbacks still pending." );
807 } else {
808 $closed = true;
809 }
810 $this->mOpened = false;
811
812 return $closed;
813 }
814
815 /**
816 * Make sure isOpen() returns true as a sanity check
817 *
818 * @throws DBUnexpectedError
819 */
820 protected function assertOpen() {
821 if ( !$this->isOpen() ) {
822 throw new DBUnexpectedError( $this, "DB connection was already closed." );
823 }
824 }
825
826 /**
827 * Closes underlying database connection
828 * @since 1.20
829 * @return bool Whether connection was closed successfully
830 */
831 abstract protected function closeConnection();
832
833 function reportConnectionError( $error = 'Unknown error' ) {
834 $myError = $this->lastError();
835 if ( $myError ) {
836 $error = $myError;
837 }
838
839 # New method
840 throw new DBConnectionError( $this, $error );
841 }
842
843 /**
844 * The DBMS-dependent part of query()
845 *
846 * @param string $sql SQL query.
847 * @return ResultWrapper|bool Result object to feed to fetchObject,
848 * fetchRow, ...; or false on failure
849 */
850 abstract protected function doQuery( $sql );
851
852 /**
853 * Determine whether a query writes to the DB.
854 * Should return true if unsure.
855 *
856 * @param string $sql
857 * @return bool
858 */
859 protected function isWriteQuery( $sql ) {
860 return !preg_match(
861 '/^(?:SELECT|BEGIN|ROLLBACK|COMMIT|SET|SHOW|EXPLAIN|\(SELECT)\b/i', $sql );
862 }
863
864 /**
865 * @param $sql
866 * @return string|null
867 */
868 protected function getQueryVerb( $sql ) {
869 return preg_match( '/^\s*([a-z]+)/i', $sql, $m ) ? strtoupper( $m[1] ) : null;
870 }
871
872 /**
873 * Determine whether a SQL statement is sensitive to isolation level.
874 * A SQL statement is considered transactable if its result could vary
875 * depending on the transaction isolation level. Operational commands
876 * such as 'SET' and 'SHOW' are not considered to be transactable.
877 *
878 * @param string $sql
879 * @return bool
880 */
881 protected function isTransactableQuery( $sql ) {
882 $verb = $this->getQueryVerb( $sql );
883 return !in_array( $verb, [ 'BEGIN', 'COMMIT', 'ROLLBACK', 'SHOW', 'SET' ], true );
884 }
885
886 public function query( $sql, $fname = __METHOD__, $tempIgnore = false ) {
887 global $wgUser;
888
889 $priorWritesPending = $this->writesOrCallbacksPending();
890 $this->mLastQuery = $sql;
891
892 $isWrite = $this->isWriteQuery( $sql );
893 if ( $isWrite ) {
894 $reason = $this->getReadOnlyReason();
895 if ( $reason !== false ) {
896 throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
897 }
898 # Set a flag indicating that writes have been done
899 $this->mDoneWrites = microtime( true );
900 }
901
902 # Add a comment for easy SHOW PROCESSLIST interpretation
903 if ( is_object( $wgUser ) && $wgUser->isItemLoaded( 'name' ) ) {
904 $userName = $wgUser->getName();
905 if ( mb_strlen( $userName ) > 15 ) {
906 $userName = mb_substr( $userName, 0, 15 ) . '...';
907 }
908 $userName = str_replace( '/', '', $userName );
909 } else {
910 $userName = '';
911 }
912
913 // Add trace comment to the begin of the sql string, right after the operator.
914 // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (bug 42598)
915 $commentedSql = preg_replace( '/\s|$/', " /* $fname $userName */ ", $sql, 1 );
916
917 # Start implicit transactions that wrap the request if DBO_TRX is enabled
918 if ( !$this->mTrxLevel && $this->getFlag( DBO_TRX )
919 && $this->isTransactableQuery( $sql )
920 ) {
921 $this->begin( __METHOD__ . " ($fname)", self::TRANSACTION_INTERNAL );
922 $this->mTrxAutomatic = true;
923 }
924
925 # Keep track of whether the transaction has write queries pending
926 if ( $this->mTrxLevel && !$this->mTrxDoneWrites && $isWrite ) {
927 $this->mTrxDoneWrites = true;
928 $this->trxProfiler->transactionWritingIn(
929 $this->mServer, $this->mDBname, $this->mTrxShortId );
930 }
931
932 if ( $this->debug() ) {
933 $this->queryLogger->debug( "{$this->mDBname} {$commentedSql}" );
934 }
935
936 # Avoid fatals if close() was called
937 $this->assertOpen();
938
939 # Send the query to the server
940 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
941
942 # Try reconnecting if the connection was lost
943 if ( false === $ret && $this->wasErrorReissuable() ) {
944 $recoverable = $this->canRecoverFromDisconnect( $sql, $priorWritesPending );
945 # Stash the last error values before anything might clear them
946 $lastError = $this->lastError();
947 $lastErrno = $this->lastErrno();
948 # Update state tracking to reflect transaction loss due to disconnection
949 $this->handleTransactionLoss();
950 if ( $this->reconnect() ) {
951 $msg = __METHOD__ . ": lost connection to {$this->getServer()}; reconnected";
952 $this->connLogger->warning( $msg );
953 $this->queryLogger->warning(
954 "$msg:\n" . ( new RuntimeException() )->getTraceAsString() );
955
956 if ( !$recoverable ) {
957 # Callers may catch the exception and continue to use the DB
958 $this->reportQueryError( $lastError, $lastErrno, $sql, $fname );
959 } else {
960 # Should be safe to silently retry the query
961 $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
962 }
963 } else {
964 $msg = __METHOD__ . ": lost connection to {$this->getServer()} permanently";
965 $this->connLogger->error( $msg );
966 }
967 }
968
969 if ( false === $ret ) {
970 # Deadlocks cause the entire transaction to abort, not just the statement.
971 # http://dev.mysql.com/doc/refman/5.7/en/innodb-error-handling.html
972 # https://www.postgresql.org/docs/9.1/static/explicit-locking.html
973 if ( $this->wasDeadlock() ) {
974 if ( $this->explicitTrxActive() || $priorWritesPending ) {
975 $tempIgnore = false; // not recoverable
976 }
977 # Update state tracking to reflect transaction loss
978 $this->handleTransactionLoss();
979 }
980
981 $this->reportQueryError(
982 $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore );
983 }
984
985 $res = $this->resultObject( $ret );
986
987 return $res;
988 }
989
990 private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) {
991 $isMaster = !is_null( $this->getLBInfo( 'master' ) );
992 # generalizeSQL() will probably cut down the query to reasonable
993 # logging size most of the time. The substr is really just a sanity check.
994 if ( $isMaster ) {
995 $queryProf = 'query-m: ' . substr( self::generalizeSQL( $sql ), 0, 255 );
996 } else {
997 $queryProf = 'query: ' . substr( self::generalizeSQL( $sql ), 0, 255 );
998 }
999
1000 # Include query transaction state
1001 $queryProf .= $this->mTrxShortId ? " [TRX#{$this->mTrxShortId}]" : "";
1002
1003 $startTime = microtime( true );
1004 $this->profiler->profileIn( $queryProf );
1005 $ret = $this->doQuery( $commentedSql );
1006 $this->profiler->profileOut( $queryProf );
1007 $queryRuntime = max( microtime( true ) - $startTime, 0.0 );
1008
1009 unset( $queryProfSection ); // profile out (if set)
1010
1011 if ( $ret !== false ) {
1012 $this->lastPing = $startTime;
1013 if ( $isWrite && $this->mTrxLevel ) {
1014 $this->updateTrxWriteQueryTime( $sql, $queryRuntime );
1015 $this->mTrxWriteCallers[] = $fname;
1016 }
1017 }
1018
1019 if ( $sql === self::PING_QUERY ) {
1020 $this->mRTTEstimate = $queryRuntime;
1021 }
1022
1023 $this->trxProfiler->recordQueryCompletion(
1024 $queryProf, $startTime, $isWrite, $this->affectedRows()
1025 );
1026 MWDebug::query( $sql, $fname, $isMaster, $queryRuntime );
1027
1028 return $ret;
1029 }
1030
1031 /**
1032 * Update the estimated run-time of a query, not counting large row lock times
1033 *
1034 * LoadBalancer can be set to rollback transactions that will create huge replication
1035 * lag. It bases this estimate off of pendingWriteQueryDuration(). Certain simple
1036 * queries, like inserting a row can take a long time due to row locking. This method
1037 * uses some simple heuristics to discount those cases.
1038 *
1039 * @param string $sql A SQL write query
1040 * @param float $runtime Total runtime, including RTT
1041 */
1042 private function updateTrxWriteQueryTime( $sql, $runtime ) {
1043 // Whether this is indicative of replica DB runtime (except for RBR or ws_repl)
1044 $indicativeOfReplicaRuntime = true;
1045 if ( $runtime > self::SLOW_WRITE_SEC ) {
1046 $verb = $this->getQueryVerb( $sql );
1047 // insert(), upsert(), replace() are fast unless bulky in size or blocked on locks
1048 if ( $verb === 'INSERT' ) {
1049 $indicativeOfReplicaRuntime = $this->affectedRows() > self::SMALL_WRITE_ROWS;
1050 } elseif ( $verb === 'REPLACE' ) {
1051 $indicativeOfReplicaRuntime = $this->affectedRows() > self::SMALL_WRITE_ROWS / 2;
1052 }
1053 }
1054
1055 $this->mTrxWriteDuration += $runtime;
1056 $this->mTrxWriteQueryCount += 1;
1057 if ( $indicativeOfReplicaRuntime ) {
1058 $this->mTrxWriteAdjDuration += $runtime;
1059 $this->mTrxWriteAdjQueryCount += 1;
1060 }
1061 }
1062
1063 private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
1064 # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
1065 # Dropped connections also mean that named locks are automatically released.
1066 # Only allow error suppression in autocommit mode or when the lost transaction
1067 # didn't matter anyway (aside from DBO_TRX snapshot loss).
1068 if ( $this->mNamedLocksHeld ) {
1069 return false; // possible critical section violation
1070 } elseif ( $sql === 'COMMIT' ) {
1071 return !$priorWritesPending; // nothing written anyway? (T127428)
1072 } elseif ( $sql === 'ROLLBACK' ) {
1073 return true; // transaction lost...which is also what was requested :)
1074 } elseif ( $this->explicitTrxActive() ) {
1075 return false; // don't drop atomocity
1076 } elseif ( $priorWritesPending ) {
1077 return false; // prior writes lost from implicit transaction
1078 }
1079
1080 return true;
1081 }
1082
1083 private function handleTransactionLoss() {
1084 $this->mTrxLevel = 0;
1085 $this->mTrxIdleCallbacks = []; // bug 65263
1086 $this->mTrxPreCommitCallbacks = []; // bug 65263
1087 try {
1088 // Handle callbacks in mTrxEndCallbacks
1089 $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
1090 $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
1091 return null;
1092 } catch ( Exception $e ) {
1093 // Already logged; move on...
1094 return $e;
1095 }
1096 }
1097
1098 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
1099 if ( $this->ignoreErrors() || $tempIgnore ) {
1100 $this->queryLogger->debug( "SQL ERROR (ignored): $error\n" );
1101 } else {
1102 $sql1line = mb_substr( str_replace( "\n", "\\n", $sql ), 0, 5 * 1024 );
1103 $this->queryLogger->error(
1104 "{fname}\t{db_server}\t{errno}\t{error}\t{sql1line}",
1105 $this->getLogContext( [
1106 'method' => __METHOD__,
1107 'errno' => $errno,
1108 'error' => $error,
1109 'sql1line' => $sql1line,
1110 'fname' => $fname,
1111 ] )
1112 );
1113 $this->queryLogger->debug( "SQL ERROR: " . $error . "\n" );
1114 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
1115 }
1116 }
1117
1118 /**
1119 * Intended to be compatible with the PEAR::DB wrapper functions.
1120 * http://pear.php.net/manual/en/package.database.db.intro-execute.php
1121 *
1122 * ? = scalar value, quoted as necessary
1123 * ! = raw SQL bit (a function for instance)
1124 * & = filename; reads the file and inserts as a blob
1125 * (we don't use this though...)
1126 *
1127 * @param string $sql
1128 * @param string $func
1129 *
1130 * @return array
1131 */
1132 protected function prepare( $sql, $func = __METHOD__ ) {
1133 /* MySQL doesn't support prepared statements (yet), so just
1134 * pack up the query for reference. We'll manually replace
1135 * the bits later.
1136 */
1137 return [ 'query' => $sql, 'func' => $func ];
1138 }
1139
1140 /**
1141 * Free a prepared query, generated by prepare().
1142 * @param string $prepared
1143 */
1144 protected function freePrepared( $prepared ) {
1145 /* No-op by default */
1146 }
1147
1148 /**
1149 * Execute a prepared query with the various arguments
1150 * @param string $prepared The prepared sql
1151 * @param mixed $args Either an array here, or put scalars as varargs
1152 *
1153 * @return ResultWrapper
1154 */
1155 public function execute( $prepared, $args = null ) {
1156 if ( !is_array( $args ) ) {
1157 # Pull the var args
1158 $args = func_get_args();
1159 array_shift( $args );
1160 }
1161
1162 $sql = $this->fillPrepared( $prepared['query'], $args );
1163
1164 return $this->query( $sql, $prepared['func'] );
1165 }
1166
1167 /**
1168 * For faking prepared SQL statements on DBs that don't support it directly.
1169 *
1170 * @param string $preparedQuery A 'preparable' SQL statement
1171 * @param array $args Array of Arguments to fill it with
1172 * @return string Executable SQL
1173 */
1174 public function fillPrepared( $preparedQuery, $args ) {
1175 reset( $args );
1176 $this->preparedArgs =& $args;
1177
1178 return preg_replace_callback( '/(\\\\[?!&]|[?!&])/',
1179 [ &$this, 'fillPreparedArg' ], $preparedQuery );
1180 }
1181
1182 /**
1183 * preg_callback func for fillPrepared()
1184 * The arguments should be in $this->preparedArgs and must not be touched
1185 * while we're doing this.
1186 *
1187 * @param array $matches
1188 * @throws DBUnexpectedError
1189 * @return string
1190 */
1191 protected function fillPreparedArg( $matches ) {
1192 switch ( $matches[1] ) {
1193 case '\\?':
1194 return '?';
1195 case '\\!':
1196 return '!';
1197 case '\\&':
1198 return '&';
1199 }
1200
1201 list( /* $n */, $arg ) = each( $this->preparedArgs );
1202
1203 switch ( $matches[1] ) {
1204 case '?':
1205 return $this->addQuotes( $arg );
1206 case '!':
1207 return $arg;
1208 case '&':
1209 # return $this->addQuotes( file_get_contents( $arg ) );
1210 throw new DBUnexpectedError(
1211 $this,
1212 '& mode is not implemented. If it\'s really needed, uncomment the line above.'
1213 );
1214 default:
1215 throw new DBUnexpectedError(
1216 $this,
1217 'Received invalid match. This should never happen!'
1218 );
1219 }
1220 }
1221
1222 public function freeResult( $res ) {
1223 }
1224
1225 public function selectField(
1226 $table, $var, $cond = '', $fname = __METHOD__, $options = []
1227 ) {
1228 if ( $var === '*' ) { // sanity
1229 throw new DBUnexpectedError( $this, "Cannot use a * field: got '$var'" );
1230 }
1231
1232 if ( !is_array( $options ) ) {
1233 $options = [ $options ];
1234 }
1235
1236 $options['LIMIT'] = 1;
1237
1238 $res = $this->select( $table, $var, $cond, $fname, $options );
1239 if ( $res === false || !$this->numRows( $res ) ) {
1240 return false;
1241 }
1242
1243 $row = $this->fetchRow( $res );
1244
1245 if ( $row !== false ) {
1246 return reset( $row );
1247 } else {
1248 return false;
1249 }
1250 }
1251
1252 public function selectFieldValues(
1253 $table, $var, $cond = '', $fname = __METHOD__, $options = [], $join_conds = []
1254 ) {
1255 if ( $var === '*' ) { // sanity
1256 throw new DBUnexpectedError( $this, "Cannot use a * field" );
1257 } elseif ( !is_string( $var ) ) { // sanity
1258 throw new DBUnexpectedError( $this, "Cannot use an array of fields" );
1259 }
1260
1261 if ( !is_array( $options ) ) {
1262 $options = [ $options ];
1263 }
1264
1265 $res = $this->select( $table, $var, $cond, $fname, $options, $join_conds );
1266 if ( $res === false ) {
1267 return false;
1268 }
1269
1270 $values = [];
1271 foreach ( $res as $row ) {
1272 $values[] = $row->$var;
1273 }
1274
1275 return $values;
1276 }
1277
1278 /**
1279 * Returns an optional USE INDEX clause to go after the table, and a
1280 * string to go at the end of the query.
1281 *
1282 * @param array $options Associative array of options to be turned into
1283 * an SQL query, valid keys are listed in the function.
1284 * @return array
1285 * @see DatabaseBase::select()
1286 */
1287 public function makeSelectOptions( $options ) {
1288 $preLimitTail = $postLimitTail = '';
1289 $startOpts = '';
1290
1291 $noKeyOptions = [];
1292
1293 foreach ( $options as $key => $option ) {
1294 if ( is_numeric( $key ) ) {
1295 $noKeyOptions[$option] = true;
1296 }
1297 }
1298
1299 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1300
1301 $preLimitTail .= $this->makeOrderBy( $options );
1302
1303 // if (isset($options['LIMIT'])) {
1304 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
1305 // isset($options['OFFSET']) ? $options['OFFSET']
1306 // : false);
1307 // }
1308
1309 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1310 $postLimitTail .= ' FOR UPDATE';
1311 }
1312
1313 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1314 $postLimitTail .= ' LOCK IN SHARE MODE';
1315 }
1316
1317 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1318 $startOpts .= 'DISTINCT';
1319 }
1320
1321 # Various MySQL extensions
1322 if ( isset( $noKeyOptions['STRAIGHT_JOIN'] ) ) {
1323 $startOpts .= ' /*! STRAIGHT_JOIN */';
1324 }
1325
1326 if ( isset( $noKeyOptions['HIGH_PRIORITY'] ) ) {
1327 $startOpts .= ' HIGH_PRIORITY';
1328 }
1329
1330 if ( isset( $noKeyOptions['SQL_BIG_RESULT'] ) ) {
1331 $startOpts .= ' SQL_BIG_RESULT';
1332 }
1333
1334 if ( isset( $noKeyOptions['SQL_BUFFER_RESULT'] ) ) {
1335 $startOpts .= ' SQL_BUFFER_RESULT';
1336 }
1337
1338 if ( isset( $noKeyOptions['SQL_SMALL_RESULT'] ) ) {
1339 $startOpts .= ' SQL_SMALL_RESULT';
1340 }
1341
1342 if ( isset( $noKeyOptions['SQL_CALC_FOUND_ROWS'] ) ) {
1343 $startOpts .= ' SQL_CALC_FOUND_ROWS';
1344 }
1345
1346 if ( isset( $noKeyOptions['SQL_CACHE'] ) ) {
1347 $startOpts .= ' SQL_CACHE';
1348 }
1349
1350 if ( isset( $noKeyOptions['SQL_NO_CACHE'] ) ) {
1351 $startOpts .= ' SQL_NO_CACHE';
1352 }
1353
1354 if ( isset( $options['USE INDEX'] ) && is_string( $options['USE INDEX'] ) ) {
1355 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1356 } else {
1357 $useIndex = '';
1358 }
1359 if ( isset( $options['IGNORE INDEX'] ) && is_string( $options['IGNORE INDEX'] ) ) {
1360 $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
1361 } else {
1362 $ignoreIndex = '';
1363 }
1364
1365 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1366 }
1367
1368 /**
1369 * Returns an optional GROUP BY with an optional HAVING
1370 *
1371 * @param array $options Associative array of options
1372 * @return string
1373 * @see DatabaseBase::select()
1374 * @since 1.21
1375 */
1376 public function makeGroupByWithHaving( $options ) {
1377 $sql = '';
1378 if ( isset( $options['GROUP BY'] ) ) {
1379 $gb = is_array( $options['GROUP BY'] )
1380 ? implode( ',', $options['GROUP BY'] )
1381 : $options['GROUP BY'];
1382 $sql .= ' GROUP BY ' . $gb;
1383 }
1384 if ( isset( $options['HAVING'] ) ) {
1385 $having = is_array( $options['HAVING'] )
1386 ? $this->makeList( $options['HAVING'], LIST_AND )
1387 : $options['HAVING'];
1388 $sql .= ' HAVING ' . $having;
1389 }
1390
1391 return $sql;
1392 }
1393
1394 /**
1395 * Returns an optional ORDER BY
1396 *
1397 * @param array $options Associative array of options
1398 * @return string
1399 * @see DatabaseBase::select()
1400 * @since 1.21
1401 */
1402 public function makeOrderBy( $options ) {
1403 if ( isset( $options['ORDER BY'] ) ) {
1404 $ob = is_array( $options['ORDER BY'] )
1405 ? implode( ',', $options['ORDER BY'] )
1406 : $options['ORDER BY'];
1407
1408 return ' ORDER BY ' . $ob;
1409 }
1410
1411 return '';
1412 }
1413
1414 // See IDatabase::select for the docs for this function
1415 public function select( $table, $vars, $conds = '', $fname = __METHOD__,
1416 $options = [], $join_conds = [] ) {
1417 $sql = $this->selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
1418
1419 return $this->query( $sql, $fname );
1420 }
1421
1422 public function selectSQLText( $table, $vars, $conds = '', $fname = __METHOD__,
1423 $options = [], $join_conds = []
1424 ) {
1425 if ( is_array( $vars ) ) {
1426 $vars = implode( ',', $this->fieldNamesWithAlias( $vars ) );
1427 }
1428
1429 $options = (array)$options;
1430 $useIndexes = ( isset( $options['USE INDEX'] ) && is_array( $options['USE INDEX'] ) )
1431 ? $options['USE INDEX']
1432 : [];
1433 $ignoreIndexes = ( isset( $options['IGNORE INDEX'] ) && is_array( $options['IGNORE INDEX'] ) )
1434 ? $options['IGNORE INDEX']
1435 : [];
1436
1437 if ( is_array( $table ) ) {
1438 $from = ' FROM ' .
1439 $this->tableNamesWithIndexClauseOrJOIN( $table, $useIndexes, $ignoreIndexes, $join_conds );
1440 } elseif ( $table != '' ) {
1441 if ( $table[0] == ' ' ) {
1442 $from = ' FROM ' . $table;
1443 } else {
1444 $from = ' FROM ' .
1445 $this->tableNamesWithIndexClauseOrJOIN( [ $table ], $useIndexes, $ignoreIndexes, [] );
1446 }
1447 } else {
1448 $from = '';
1449 }
1450
1451 list( $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ) =
1452 $this->makeSelectOptions( $options );
1453
1454 if ( !empty( $conds ) ) {
1455 if ( is_array( $conds ) ) {
1456 $conds = $this->makeList( $conds, LIST_AND );
1457 }
1458 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex WHERE $conds $preLimitTail";
1459 } else {
1460 $sql = "SELECT $startOpts $vars $from $useIndex $ignoreIndex $preLimitTail";
1461 }
1462
1463 if ( isset( $options['LIMIT'] ) ) {
1464 $sql = $this->limitResult( $sql, $options['LIMIT'],
1465 isset( $options['OFFSET'] ) ? $options['OFFSET'] : false );
1466 }
1467 $sql = "$sql $postLimitTail";
1468
1469 if ( isset( $options['EXPLAIN'] ) ) {
1470 $sql = 'EXPLAIN ' . $sql;
1471 }
1472
1473 return $sql;
1474 }
1475
1476 public function selectRow( $table, $vars, $conds, $fname = __METHOD__,
1477 $options = [], $join_conds = []
1478 ) {
1479 $options = (array)$options;
1480 $options['LIMIT'] = 1;
1481 $res = $this->select( $table, $vars, $conds, $fname, $options, $join_conds );
1482
1483 if ( $res === false ) {
1484 return false;
1485 }
1486
1487 if ( !$this->numRows( $res ) ) {
1488 return false;
1489 }
1490
1491 $obj = $this->fetchObject( $res );
1492
1493 return $obj;
1494 }
1495
1496 public function estimateRowCount(
1497 $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = []
1498 ) {
1499 $rows = 0;
1500 $res = $this->select( $table, [ 'rowcount' => 'COUNT(*)' ], $conds, $fname, $options );
1501
1502 if ( $res ) {
1503 $row = $this->fetchRow( $res );
1504 $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
1505 }
1506
1507 return $rows;
1508 }
1509
1510 public function selectRowCount(
1511 $tables, $vars = '*', $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
1512 ) {
1513 $rows = 0;
1514 $sql = $this->selectSQLText( $tables, '1', $conds, $fname, $options, $join_conds );
1515 $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count", $fname );
1516
1517 if ( $res ) {
1518 $row = $this->fetchRow( $res );
1519 $rows = ( isset( $row['rowcount'] ) ) ? (int)$row['rowcount'] : 0;
1520 }
1521
1522 return $rows;
1523 }
1524
1525 /**
1526 * Removes most variables from an SQL query and replaces them with X or N for numbers.
1527 * It's only slightly flawed. Don't use for anything important.
1528 *
1529 * @param string $sql A SQL Query
1530 *
1531 * @return string
1532 */
1533 protected static function generalizeSQL( $sql ) {
1534 # This does the same as the regexp below would do, but in such a way
1535 # as to avoid crashing php on some large strings.
1536 # $sql = preg_replace( "/'([^\\\\']|\\\\.)*'|\"([^\\\\\"]|\\\\.)*\"/", "'X'", $sql );
1537
1538 $sql = str_replace( "\\\\", '', $sql );
1539 $sql = str_replace( "\\'", '', $sql );
1540 $sql = str_replace( "\\\"", '', $sql );
1541 $sql = preg_replace( "/'.*'/s", "'X'", $sql );
1542 $sql = preg_replace( '/".*"/s', "'X'", $sql );
1543
1544 # All newlines, tabs, etc replaced by single space
1545 $sql = preg_replace( '/\s+/', ' ', $sql );
1546
1547 # All numbers => N,
1548 # except the ones surrounded by characters, e.g. l10n
1549 $sql = preg_replace( '/-?\d+(,-?\d+)+/s', 'N,...,N', $sql );
1550 $sql = preg_replace( '/(?<![a-zA-Z])-?\d+(?![a-zA-Z])/s', 'N', $sql );
1551
1552 return $sql;
1553 }
1554
1555 public function fieldExists( $table, $field, $fname = __METHOD__ ) {
1556 $info = $this->fieldInfo( $table, $field );
1557
1558 return (bool)$info;
1559 }
1560
1561 public function indexExists( $table, $index, $fname = __METHOD__ ) {
1562 if ( !$this->tableExists( $table ) ) {
1563 return null;
1564 }
1565
1566 $info = $this->indexInfo( $table, $index, $fname );
1567 if ( is_null( $info ) ) {
1568 return null;
1569 } else {
1570 return $info !== false;
1571 }
1572 }
1573
1574 public function tableExists( $table, $fname = __METHOD__ ) {
1575 $table = $this->tableName( $table );
1576 $old = $this->ignoreErrors( true );
1577 $res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname );
1578 $this->ignoreErrors( $old );
1579
1580 return (bool)$res;
1581 }
1582
1583 public function indexUnique( $table, $index ) {
1584 $indexInfo = $this->indexInfo( $table, $index );
1585
1586 if ( !$indexInfo ) {
1587 return null;
1588 }
1589
1590 return !$indexInfo[0]->Non_unique;
1591 }
1592
1593 /**
1594 * Helper for DatabaseBase::insert().
1595 *
1596 * @param array $options
1597 * @return string
1598 */
1599 protected function makeInsertOptions( $options ) {
1600 return implode( ' ', $options );
1601 }
1602
1603 public function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
1604 # No rows to insert, easy just return now
1605 if ( !count( $a ) ) {
1606 return true;
1607 }
1608
1609 $table = $this->tableName( $table );
1610
1611 if ( !is_array( $options ) ) {
1612 $options = [ $options ];
1613 }
1614
1615 $fh = null;
1616 if ( isset( $options['fileHandle'] ) ) {
1617 $fh = $options['fileHandle'];
1618 }
1619 $options = $this->makeInsertOptions( $options );
1620
1621 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
1622 $multi = true;
1623 $keys = array_keys( $a[0] );
1624 } else {
1625 $multi = false;
1626 $keys = array_keys( $a );
1627 }
1628
1629 $sql = 'INSERT ' . $options .
1630 " INTO $table (" . implode( ',', $keys ) . ') VALUES ';
1631
1632 if ( $multi ) {
1633 $first = true;
1634 foreach ( $a as $row ) {
1635 if ( $first ) {
1636 $first = false;
1637 } else {
1638 $sql .= ',';
1639 }
1640 $sql .= '(' . $this->makeList( $row ) . ')';
1641 }
1642 } else {
1643 $sql .= '(' . $this->makeList( $a ) . ')';
1644 }
1645
1646 if ( $fh !== null && false === fwrite( $fh, $sql ) ) {
1647 return false;
1648 } elseif ( $fh !== null ) {
1649 return true;
1650 }
1651
1652 return (bool)$this->query( $sql, $fname );
1653 }
1654
1655 /**
1656 * Make UPDATE options array for DatabaseBase::makeUpdateOptions
1657 *
1658 * @param array $options
1659 * @return array
1660 */
1661 protected function makeUpdateOptionsArray( $options ) {
1662 if ( !is_array( $options ) ) {
1663 $options = [ $options ];
1664 }
1665
1666 $opts = [];
1667
1668 if ( in_array( 'LOW_PRIORITY', $options ) ) {
1669 $opts[] = $this->lowPriorityOption();
1670 }
1671
1672 if ( in_array( 'IGNORE', $options ) ) {
1673 $opts[] = 'IGNORE';
1674 }
1675
1676 return $opts;
1677 }
1678
1679 /**
1680 * Make UPDATE options for the DatabaseBase::update function
1681 *
1682 * @param array $options The options passed to DatabaseBase::update
1683 * @return string
1684 */
1685 protected function makeUpdateOptions( $options ) {
1686 $opts = $this->makeUpdateOptionsArray( $options );
1687
1688 return implode( ' ', $opts );
1689 }
1690
1691 function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
1692 $table = $this->tableName( $table );
1693 $opts = $this->makeUpdateOptions( $options );
1694 $sql = "UPDATE $opts $table SET " . $this->makeList( $values, LIST_SET );
1695
1696 if ( $conds !== [] && $conds !== '*' ) {
1697 $sql .= " WHERE " . $this->makeList( $conds, LIST_AND );
1698 }
1699
1700 return $this->query( $sql, $fname );
1701 }
1702
1703 public function makeList( $a, $mode = LIST_COMMA ) {
1704 if ( !is_array( $a ) ) {
1705 throw new DBUnexpectedError( $this, __METHOD__ . ' called with incorrect parameters' );
1706 }
1707
1708 $first = true;
1709 $list = '';
1710
1711 foreach ( $a as $field => $value ) {
1712 if ( !$first ) {
1713 if ( $mode == LIST_AND ) {
1714 $list .= ' AND ';
1715 } elseif ( $mode == LIST_OR ) {
1716 $list .= ' OR ';
1717 } else {
1718 $list .= ',';
1719 }
1720 } else {
1721 $first = false;
1722 }
1723
1724 if ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_numeric( $field ) ) {
1725 $list .= "($value)";
1726 } elseif ( ( $mode == LIST_SET ) && is_numeric( $field ) ) {
1727 $list .= "$value";
1728 } elseif ( ( $mode == LIST_AND || $mode == LIST_OR ) && is_array( $value ) ) {
1729 // Remove null from array to be handled separately if found
1730 $includeNull = false;
1731 foreach ( array_keys( $value, null, true ) as $nullKey ) {
1732 $includeNull = true;
1733 unset( $value[$nullKey] );
1734 }
1735 if ( count( $value ) == 0 && !$includeNull ) {
1736 throw new InvalidArgumentException( __METHOD__ . ": empty input for field $field" );
1737 } elseif ( count( $value ) == 0 ) {
1738 // only check if $field is null
1739 $list .= "$field IS NULL";
1740 } else {
1741 // IN clause contains at least one valid element
1742 if ( $includeNull ) {
1743 // Group subconditions to ensure correct precedence
1744 $list .= '(';
1745 }
1746 if ( count( $value ) == 1 ) {
1747 // Special-case single values, as IN isn't terribly efficient
1748 // Don't necessarily assume the single key is 0; we don't
1749 // enforce linear numeric ordering on other arrays here.
1750 $value = array_values( $value )[0];
1751 $list .= $field . " = " . $this->addQuotes( $value );
1752 } else {
1753 $list .= $field . " IN (" . $this->makeList( $value ) . ") ";
1754 }
1755 // if null present in array, append IS NULL
1756 if ( $includeNull ) {
1757 $list .= " OR $field IS NULL)";
1758 }
1759 }
1760 } elseif ( $value === null ) {
1761 if ( $mode == LIST_AND || $mode == LIST_OR ) {
1762 $list .= "$field IS ";
1763 } elseif ( $mode == LIST_SET ) {
1764 $list .= "$field = ";
1765 }
1766 $list .= 'NULL';
1767 } else {
1768 if ( $mode == LIST_AND || $mode == LIST_OR || $mode == LIST_SET ) {
1769 $list .= "$field = ";
1770 }
1771 $list .= $mode == LIST_NAMES ? $value : $this->addQuotes( $value );
1772 }
1773 }
1774
1775 return $list;
1776 }
1777
1778 public function makeWhereFrom2d( $data, $baseKey, $subKey ) {
1779 $conds = [];
1780
1781 foreach ( $data as $base => $sub ) {
1782 if ( count( $sub ) ) {
1783 $conds[] = $this->makeList(
1784 [ $baseKey => $base, $subKey => array_keys( $sub ) ],
1785 LIST_AND );
1786 }
1787 }
1788
1789 if ( $conds ) {
1790 return $this->makeList( $conds, LIST_OR );
1791 } else {
1792 // Nothing to search for...
1793 return false;
1794 }
1795 }
1796
1797 /**
1798 * Return aggregated value alias
1799 *
1800 * @param array $valuedata
1801 * @param string $valuename
1802 *
1803 * @return string
1804 */
1805 public function aggregateValue( $valuedata, $valuename = 'value' ) {
1806 return $valuename;
1807 }
1808
1809 public function bitNot( $field ) {
1810 return "(~$field)";
1811 }
1812
1813 public function bitAnd( $fieldLeft, $fieldRight ) {
1814 return "($fieldLeft & $fieldRight)";
1815 }
1816
1817 public function bitOr( $fieldLeft, $fieldRight ) {
1818 return "($fieldLeft | $fieldRight)";
1819 }
1820
1821 public function buildConcat( $stringList ) {
1822 return 'CONCAT(' . implode( ',', $stringList ) . ')';
1823 }
1824
1825 public function buildGroupConcatField(
1826 $delim, $table, $field, $conds = '', $join_conds = []
1827 ) {
1828 $fld = "GROUP_CONCAT($field SEPARATOR " . $this->addQuotes( $delim ) . ')';
1829
1830 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1831 }
1832
1833 /**
1834 * @param string $field Field or column to cast
1835 * @return string
1836 * @since 1.28
1837 */
1838 public function buildStringCast( $field ) {
1839 return $field;
1840 }
1841
1842 public function selectDB( $db ) {
1843 # Stub. Shouldn't cause serious problems if it's not overridden, but
1844 # if your database engine supports a concept similar to MySQL's
1845 # databases you may as well.
1846 $this->mDBname = $db;
1847
1848 return true;
1849 }
1850
1851 public function getDBname() {
1852 return $this->mDBname;
1853 }
1854
1855 public function getServer() {
1856 return $this->mServer;
1857 }
1858
1859 /**
1860 * Format a table name ready for use in constructing an SQL query
1861 *
1862 * This does two important things: it quotes the table names to clean them up,
1863 * and it adds a table prefix if only given a table name with no quotes.
1864 *
1865 * All functions of this object which require a table name call this function
1866 * themselves. Pass the canonical name to such functions. This is only needed
1867 * when calling query() directly.
1868 *
1869 * @note This function does not sanitize user input. It is not safe to use
1870 * this function to escape user input.
1871 * @param string $name Database table name
1872 * @param string $format One of:
1873 * quoted - Automatically pass the table name through addIdentifierQuotes()
1874 * so that it can be used in a query.
1875 * raw - Do not add identifier quotes to the table name
1876 * @return string Full database name
1877 */
1878 public function tableName( $name, $format = 'quoted' ) {
1879 global $wgSharedDB, $wgSharedPrefix, $wgSharedTables, $wgSharedSchema;
1880 # Skip the entire process when we have a string quoted on both ends.
1881 # Note that we check the end so that we will still quote any use of
1882 # use of `database`.table. But won't break things if someone wants
1883 # to query a database table with a dot in the name.
1884 if ( $this->isQuotedIdentifier( $name ) ) {
1885 return $name;
1886 }
1887
1888 # Lets test for any bits of text that should never show up in a table
1889 # name. Basically anything like JOIN or ON which are actually part of
1890 # SQL queries, but may end up inside of the table value to combine
1891 # sql. Such as how the API is doing.
1892 # Note that we use a whitespace test rather than a \b test to avoid
1893 # any remote case where a word like on may be inside of a table name
1894 # surrounded by symbols which may be considered word breaks.
1895 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
1896 return $name;
1897 }
1898
1899 # Split database and table into proper variables.
1900 # We reverse the explode so that database.table and table both output
1901 # the correct table.
1902 $dbDetails = explode( '.', $name, 3 );
1903 if ( count( $dbDetails ) == 3 ) {
1904 list( $database, $schema, $table ) = $dbDetails;
1905 # We don't want any prefix added in this case
1906 $prefix = '';
1907 } elseif ( count( $dbDetails ) == 2 ) {
1908 list( $database, $table ) = $dbDetails;
1909 # We don't want any prefix added in this case
1910 # In dbs that support it, $database may actually be the schema
1911 # but that doesn't affect any of the functionality here
1912 $prefix = '';
1913 $schema = null;
1914 } else {
1915 list( $table ) = $dbDetails;
1916 if ( $wgSharedDB !== null # We have a shared database
1917 && $this->mForeign == false # We're not working on a foreign database
1918 && !$this->isQuotedIdentifier( $table ) # Prevent shared tables listing '`table`'
1919 && in_array( $table, $wgSharedTables ) # A shared table is selected
1920 ) {
1921 $database = $wgSharedDB;
1922 $schema = $wgSharedSchema === null ? $this->mSchema : $wgSharedSchema;
1923 $prefix = $wgSharedPrefix === null ? $this->mTablePrefix : $wgSharedPrefix;
1924 } else {
1925 $database = null;
1926 $schema = $this->mSchema; # Default schema
1927 $prefix = $this->mTablePrefix; # Default prefix
1928 }
1929 }
1930
1931 # Quote $table and apply the prefix if not quoted.
1932 # $tableName might be empty if this is called from Database::replaceVars()
1933 $tableName = "{$prefix}{$table}";
1934 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $tableName ) && $tableName !== '' ) {
1935 $tableName = $this->addIdentifierQuotes( $tableName );
1936 }
1937
1938 # Quote $schema and merge it with the table name if needed
1939 if ( strlen( $schema ) ) {
1940 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $schema ) ) {
1941 $schema = $this->addIdentifierQuotes( $schema );
1942 }
1943 $tableName = $schema . '.' . $tableName;
1944 }
1945
1946 # Quote $database and merge it with the table name if needed
1947 if ( $database !== null ) {
1948 if ( $format == 'quoted' && !$this->isQuotedIdentifier( $database ) ) {
1949 $database = $this->addIdentifierQuotes( $database );
1950 }
1951 $tableName = $database . '.' . $tableName;
1952 }
1953
1954 return $tableName;
1955 }
1956
1957 /**
1958 * Fetch a number of table names into an array
1959 * This is handy when you need to construct SQL for joins
1960 *
1961 * Example:
1962 * extract( $dbr->tableNames( 'user', 'watchlist' ) );
1963 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1964 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1965 *
1966 * @return array
1967 */
1968 public function tableNames() {
1969 $inArray = func_get_args();
1970 $retVal = [];
1971
1972 foreach ( $inArray as $name ) {
1973 $retVal[$name] = $this->tableName( $name );
1974 }
1975
1976 return $retVal;
1977 }
1978
1979 /**
1980 * Fetch a number of table names into an zero-indexed numerical array
1981 * This is handy when you need to construct SQL for joins
1982 *
1983 * Example:
1984 * list( $user, $watchlist ) = $dbr->tableNamesN( 'user', 'watchlist' );
1985 * $sql = "SELECT wl_namespace,wl_title FROM $watchlist,$user
1986 * WHERE wl_user=user_id AND wl_user=$nameWithQuotes";
1987 *
1988 * @return array
1989 */
1990 public function tableNamesN() {
1991 $inArray = func_get_args();
1992 $retVal = [];
1993
1994 foreach ( $inArray as $name ) {
1995 $retVal[] = $this->tableName( $name );
1996 }
1997
1998 return $retVal;
1999 }
2000
2001 /**
2002 * Get an aliased table name
2003 * e.g. tableName AS newTableName
2004 *
2005 * @param string $name Table name, see tableName()
2006 * @param string|bool $alias Alias (optional)
2007 * @return string SQL name for aliased table. Will not alias a table to its own name
2008 */
2009 public function tableNameWithAlias( $name, $alias = false ) {
2010 if ( !$alias || $alias == $name ) {
2011 return $this->tableName( $name );
2012 } else {
2013 return $this->tableName( $name ) . ' ' . $this->addIdentifierQuotes( $alias );
2014 }
2015 }
2016
2017 /**
2018 * Gets an array of aliased table names
2019 *
2020 * @param array $tables [ [alias] => table ]
2021 * @return string[] See tableNameWithAlias()
2022 */
2023 public function tableNamesWithAlias( $tables ) {
2024 $retval = [];
2025 foreach ( $tables as $alias => $table ) {
2026 if ( is_numeric( $alias ) ) {
2027 $alias = $table;
2028 }
2029 $retval[] = $this->tableNameWithAlias( $table, $alias );
2030 }
2031
2032 return $retval;
2033 }
2034
2035 /**
2036 * Get an aliased field name
2037 * e.g. fieldName AS newFieldName
2038 *
2039 * @param string $name Field name
2040 * @param string|bool $alias Alias (optional)
2041 * @return string SQL name for aliased field. Will not alias a field to its own name
2042 */
2043 public function fieldNameWithAlias( $name, $alias = false ) {
2044 if ( !$alias || (string)$alias === (string)$name ) {
2045 return $name;
2046 } else {
2047 return $name . ' AS ' . $this->addIdentifierQuotes( $alias ); // PostgreSQL needs AS
2048 }
2049 }
2050
2051 /**
2052 * Gets an array of aliased field names
2053 *
2054 * @param array $fields [ [alias] => field ]
2055 * @return string[] See fieldNameWithAlias()
2056 */
2057 public function fieldNamesWithAlias( $fields ) {
2058 $retval = [];
2059 foreach ( $fields as $alias => $field ) {
2060 if ( is_numeric( $alias ) ) {
2061 $alias = $field;
2062 }
2063 $retval[] = $this->fieldNameWithAlias( $field, $alias );
2064 }
2065
2066 return $retval;
2067 }
2068
2069 /**
2070 * Get the aliased table name clause for a FROM clause
2071 * which might have a JOIN and/or USE INDEX or IGNORE INDEX clause
2072 *
2073 * @param array $tables ( [alias] => table )
2074 * @param array $use_index Same as for select()
2075 * @param array $ignore_index Same as for select()
2076 * @param array $join_conds Same as for select()
2077 * @return string
2078 */
2079 protected function tableNamesWithIndexClauseOrJOIN(
2080 $tables, $use_index = [], $ignore_index = [], $join_conds = []
2081 ) {
2082 $ret = [];
2083 $retJOIN = [];
2084 $use_index = (array)$use_index;
2085 $ignore_index = (array)$ignore_index;
2086 $join_conds = (array)$join_conds;
2087
2088 foreach ( $tables as $alias => $table ) {
2089 if ( !is_string( $alias ) ) {
2090 // No alias? Set it equal to the table name
2091 $alias = $table;
2092 }
2093 // Is there a JOIN clause for this table?
2094 if ( isset( $join_conds[$alias] ) ) {
2095 list( $joinType, $conds ) = $join_conds[$alias];
2096 $tableClause = $joinType;
2097 $tableClause .= ' ' . $this->tableNameWithAlias( $table, $alias );
2098 if ( isset( $use_index[$alias] ) ) { // has USE INDEX?
2099 $use = $this->useIndexClause( implode( ',', (array)$use_index[$alias] ) );
2100 if ( $use != '' ) {
2101 $tableClause .= ' ' . $use;
2102 }
2103 }
2104 if ( isset( $ignore_index[$alias] ) ) { // has IGNORE INDEX?
2105 $ignore = $this->ignoreIndexClause( implode( ',', (array)$ignore_index[$alias] ) );
2106 if ( $ignore != '' ) {
2107 $tableClause .= ' ' . $ignore;
2108 }
2109 }
2110 $on = $this->makeList( (array)$conds, LIST_AND );
2111 if ( $on != '' ) {
2112 $tableClause .= ' ON (' . $on . ')';
2113 }
2114
2115 $retJOIN[] = $tableClause;
2116 } elseif ( isset( $use_index[$alias] ) ) {
2117 // Is there an INDEX clause for this table?
2118 $tableClause = $this->tableNameWithAlias( $table, $alias );
2119 $tableClause .= ' ' . $this->useIndexClause(
2120 implode( ',', (array)$use_index[$alias] )
2121 );
2122
2123 $ret[] = $tableClause;
2124 } elseif ( isset( $ignore_index[$alias] ) ) {
2125 // Is there an INDEX clause for this table?
2126 $tableClause = $this->tableNameWithAlias( $table, $alias );
2127 $tableClause .= ' ' . $this->ignoreIndexClause(
2128 implode( ',', (array)$ignore_index[$alias] )
2129 );
2130
2131 $ret[] = $tableClause;
2132 } else {
2133 $tableClause = $this->tableNameWithAlias( $table, $alias );
2134
2135 $ret[] = $tableClause;
2136 }
2137 }
2138
2139 // We can't separate explicit JOIN clauses with ',', use ' ' for those
2140 $implicitJoins = !empty( $ret ) ? implode( ',', $ret ) : "";
2141 $explicitJoins = !empty( $retJOIN ) ? implode( ' ', $retJOIN ) : "";
2142
2143 // Compile our final table clause
2144 return implode( ' ', [ $implicitJoins, $explicitJoins ] );
2145 }
2146
2147 /**
2148 * Get the name of an index in a given table.
2149 *
2150 * @param string $index
2151 * @return string
2152 */
2153 protected function indexName( $index ) {
2154 // Backwards-compatibility hack
2155 $renamed = [
2156 'ar_usertext_timestamp' => 'usertext_timestamp',
2157 'un_user_id' => 'user_id',
2158 'un_user_ip' => 'user_ip',
2159 ];
2160
2161 if ( isset( $renamed[$index] ) ) {
2162 return $renamed[$index];
2163 } else {
2164 return $index;
2165 }
2166 }
2167
2168 public function addQuotes( $s ) {
2169 if ( $s instanceof Blob ) {
2170 $s = $s->fetch();
2171 }
2172 if ( $s === null ) {
2173 return 'NULL';
2174 } else {
2175 # This will also quote numeric values. This should be harmless,
2176 # and protects against weird problems that occur when they really
2177 # _are_ strings such as article titles and string->number->string
2178 # conversion is not 1:1.
2179 return "'" . $this->strencode( $s ) . "'";
2180 }
2181 }
2182
2183 /**
2184 * Quotes an identifier using `backticks` or "double quotes" depending on the database type.
2185 * MySQL uses `backticks` while basically everything else uses double quotes.
2186 * Since MySQL is the odd one out here the double quotes are our generic
2187 * and we implement backticks in DatabaseMysql.
2188 *
2189 * @param string $s
2190 * @return string
2191 */
2192 public function addIdentifierQuotes( $s ) {
2193 return '"' . str_replace( '"', '""', $s ) . '"';
2194 }
2195
2196 /**
2197 * Returns if the given identifier looks quoted or not according to
2198 * the database convention for quoting identifiers .
2199 *
2200 * @note Do not use this to determine if untrusted input is safe.
2201 * A malicious user can trick this function.
2202 * @param string $name
2203 * @return bool
2204 */
2205 public function isQuotedIdentifier( $name ) {
2206 return $name[0] == '"' && substr( $name, -1, 1 ) == '"';
2207 }
2208
2209 /**
2210 * @param string $s
2211 * @return string
2212 */
2213 protected function escapeLikeInternal( $s ) {
2214 return addcslashes( $s, '\%_' );
2215 }
2216
2217 public function buildLike() {
2218 $params = func_get_args();
2219
2220 if ( count( $params ) > 0 && is_array( $params[0] ) ) {
2221 $params = $params[0];
2222 }
2223
2224 $s = '';
2225
2226 foreach ( $params as $value ) {
2227 if ( $value instanceof LikeMatch ) {
2228 $s .= $value->toString();
2229 } else {
2230 $s .= $this->escapeLikeInternal( $value );
2231 }
2232 }
2233
2234 return " LIKE {$this->addQuotes( $s )} ";
2235 }
2236
2237 public function anyChar() {
2238 return new LikeMatch( '_' );
2239 }
2240
2241 public function anyString() {
2242 return new LikeMatch( '%' );
2243 }
2244
2245 public function nextSequenceValue( $seqName ) {
2246 return null;
2247 }
2248
2249 /**
2250 * USE INDEX clause. Unlikely to be useful for anything but MySQL. This
2251 * is only needed because a) MySQL must be as efficient as possible due to
2252 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2253 * which index to pick. Anyway, other databases might have different
2254 * indexes on a given table. So don't bother overriding this unless you're
2255 * MySQL.
2256 * @param string $index
2257 * @return string
2258 */
2259 public function useIndexClause( $index ) {
2260 return '';
2261 }
2262
2263 /**
2264 * IGNORE INDEX clause. Unlikely to be useful for anything but MySQL. This
2265 * is only needed because a) MySQL must be as efficient as possible due to
2266 * its use on Wikipedia, and b) MySQL 4.0 is kind of dumb sometimes about
2267 * which index to pick. Anyway, other databases might have different
2268 * indexes on a given table. So don't bother overriding this unless you're
2269 * MySQL.
2270 * @param string $index
2271 * @return string
2272 */
2273 public function ignoreIndexClause( $index ) {
2274 return '';
2275 }
2276
2277 public function replace( $table, $uniqueIndexes, $rows, $fname = __METHOD__ ) {
2278 $quotedTable = $this->tableName( $table );
2279
2280 if ( count( $rows ) == 0 ) {
2281 return;
2282 }
2283
2284 # Single row case
2285 if ( !is_array( reset( $rows ) ) ) {
2286 $rows = [ $rows ];
2287 }
2288
2289 // @FXIME: this is not atomic, but a trx would break affectedRows()
2290 foreach ( $rows as $row ) {
2291 # Delete rows which collide
2292 if ( $uniqueIndexes ) {
2293 $sql = "DELETE FROM $quotedTable WHERE ";
2294 $first = true;
2295 foreach ( $uniqueIndexes as $index ) {
2296 if ( $first ) {
2297 $first = false;
2298 $sql .= '( ';
2299 } else {
2300 $sql .= ' ) OR ( ';
2301 }
2302 if ( is_array( $index ) ) {
2303 $first2 = true;
2304 foreach ( $index as $col ) {
2305 if ( $first2 ) {
2306 $first2 = false;
2307 } else {
2308 $sql .= ' AND ';
2309 }
2310 $sql .= $col . '=' . $this->addQuotes( $row[$col] );
2311 }
2312 } else {
2313 $sql .= $index . '=' . $this->addQuotes( $row[$index] );
2314 }
2315 }
2316 $sql .= ' )';
2317 $this->query( $sql, $fname );
2318 }
2319
2320 # Now insert the row
2321 $this->insert( $table, $row, $fname );
2322 }
2323 }
2324
2325 /**
2326 * REPLACE query wrapper for MySQL and SQLite, which have a native REPLACE
2327 * statement.
2328 *
2329 * @param string $table Table name
2330 * @param array|string $rows Row(s) to insert
2331 * @param string $fname Caller function name
2332 *
2333 * @return ResultWrapper
2334 */
2335 protected function nativeReplace( $table, $rows, $fname ) {
2336 $table = $this->tableName( $table );
2337
2338 # Single row case
2339 if ( !is_array( reset( $rows ) ) ) {
2340 $rows = [ $rows ];
2341 }
2342
2343 $sql = "REPLACE INTO $table (" . implode( ',', array_keys( $rows[0] ) ) . ') VALUES ';
2344 $first = true;
2345
2346 foreach ( $rows as $row ) {
2347 if ( $first ) {
2348 $first = false;
2349 } else {
2350 $sql .= ',';
2351 }
2352
2353 $sql .= '(' . $this->makeList( $row ) . ')';
2354 }
2355
2356 return $this->query( $sql, $fname );
2357 }
2358
2359 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
2360 $fname = __METHOD__
2361 ) {
2362 if ( !count( $rows ) ) {
2363 return true; // nothing to do
2364 }
2365
2366 if ( !is_array( reset( $rows ) ) ) {
2367 $rows = [ $rows ];
2368 }
2369
2370 if ( count( $uniqueIndexes ) ) {
2371 $clauses = []; // list WHERE clauses that each identify a single row
2372 foreach ( $rows as $row ) {
2373 foreach ( $uniqueIndexes as $index ) {
2374 $index = is_array( $index ) ? $index : [ $index ]; // columns
2375 $rowKey = []; // unique key to this row
2376 foreach ( $index as $column ) {
2377 $rowKey[$column] = $row[$column];
2378 }
2379 $clauses[] = $this->makeList( $rowKey, LIST_AND );
2380 }
2381 }
2382 $where = [ $this->makeList( $clauses, LIST_OR ) ];
2383 } else {
2384 $where = false;
2385 }
2386
2387 $useTrx = !$this->mTrxLevel;
2388 if ( $useTrx ) {
2389 $this->begin( $fname, self::TRANSACTION_INTERNAL );
2390 }
2391 try {
2392 # Update any existing conflicting row(s)
2393 if ( $where !== false ) {
2394 $ok = $this->update( $table, $set, $where, $fname );
2395 } else {
2396 $ok = true;
2397 }
2398 # Now insert any non-conflicting row(s)
2399 $ok = $this->insert( $table, $rows, $fname, [ 'IGNORE' ] ) && $ok;
2400 } catch ( Exception $e ) {
2401 if ( $useTrx ) {
2402 $this->rollback( $fname, self::FLUSHING_INTERNAL );
2403 }
2404 throw $e;
2405 }
2406 if ( $useTrx ) {
2407 $this->commit( $fname, self::FLUSHING_INTERNAL );
2408 }
2409
2410 return $ok;
2411 }
2412
2413 public function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds,
2414 $fname = __METHOD__
2415 ) {
2416 if ( !$conds ) {
2417 throw new DBUnexpectedError( $this, __METHOD__ . ' called with empty $conds' );
2418 }
2419
2420 $delTable = $this->tableName( $delTable );
2421 $joinTable = $this->tableName( $joinTable );
2422 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
2423 if ( $conds != '*' ) {
2424 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
2425 }
2426 $sql .= ')';
2427
2428 $this->query( $sql, $fname );
2429 }
2430
2431 /**
2432 * Returns the size of a text field, or -1 for "unlimited"
2433 *
2434 * @param string $table
2435 * @param string $field
2436 * @return int
2437 */
2438 public function textFieldSize( $table, $field ) {
2439 $table = $this->tableName( $table );
2440 $sql = "SHOW COLUMNS FROM $table LIKE \"$field\";";
2441 $res = $this->query( $sql, __METHOD__ );
2442 $row = $this->fetchObject( $res );
2443
2444 $m = [];
2445
2446 if ( preg_match( '/\((.*)\)/', $row->Type, $m ) ) {
2447 $size = $m[1];
2448 } else {
2449 $size = -1;
2450 }
2451
2452 return $size;
2453 }
2454
2455 /**
2456 * A string to insert into queries to show that they're low-priority, like
2457 * MySQL's LOW_PRIORITY. If no such feature exists, return an empty
2458 * string and nothing bad should happen.
2459 *
2460 * @return string Returns the text of the low priority option if it is
2461 * supported, or a blank string otherwise
2462 */
2463 public function lowPriorityOption() {
2464 return '';
2465 }
2466
2467 public function delete( $table, $conds, $fname = __METHOD__ ) {
2468 if ( !$conds ) {
2469 throw new DBUnexpectedError( $this, __METHOD__ . ' called with no conditions' );
2470 }
2471
2472 $table = $this->tableName( $table );
2473 $sql = "DELETE FROM $table";
2474
2475 if ( $conds != '*' ) {
2476 if ( is_array( $conds ) ) {
2477 $conds = $this->makeList( $conds, LIST_AND );
2478 }
2479 $sql .= ' WHERE ' . $conds;
2480 }
2481
2482 return $this->query( $sql, $fname );
2483 }
2484
2485 public function insertSelect(
2486 $destTable, $srcTable, $varMap, $conds,
2487 $fname = __METHOD__, $insertOptions = [], $selectOptions = []
2488 ) {
2489 if ( $this->cliMode ) {
2490 // For massive migrations with downtime, we don't want to select everything
2491 // into memory and OOM, so do all this native on the server side if possible.
2492 return $this->nativeInsertSelect(
2493 $destTable,
2494 $srcTable,
2495 $varMap,
2496 $conds,
2497 $fname,
2498 $insertOptions,
2499 $selectOptions
2500 );
2501 }
2502
2503 // For web requests, do a locking SELECT and then INSERT. This puts the SELECT burden
2504 // on only the master (without needing row-based-replication). It also makes it easy to
2505 // know how big the INSERT is going to be.
2506 $fields = [];
2507 foreach ( $varMap as $dstColumn => $sourceColumnOrSql ) {
2508 $fields[] = $this->fieldNameWithAlias( $sourceColumnOrSql, $dstColumn );
2509 }
2510 $selectOptions[] = 'FOR UPDATE';
2511 $res = $this->select( $srcTable, implode( ',', $fields ), $conds, $fname, $selectOptions );
2512 if ( !$res ) {
2513 return false;
2514 }
2515
2516 $rows = [];
2517 foreach ( $res as $row ) {
2518 $rows[] = (array)$row;
2519 }
2520
2521 return $this->insert( $destTable, $rows, $fname, $insertOptions );
2522 }
2523
2524 public function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
2525 $fname = __METHOD__,
2526 $insertOptions = [], $selectOptions = []
2527 ) {
2528 $destTable = $this->tableName( $destTable );
2529
2530 if ( !is_array( $insertOptions ) ) {
2531 $insertOptions = [ $insertOptions ];
2532 }
2533
2534 $insertOptions = $this->makeInsertOptions( $insertOptions );
2535
2536 if ( !is_array( $selectOptions ) ) {
2537 $selectOptions = [ $selectOptions ];
2538 }
2539
2540 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) = $this->makeSelectOptions(
2541 $selectOptions );
2542
2543 if ( is_array( $srcTable ) ) {
2544 $srcTable = implode( ',', array_map( [ &$this, 'tableName' ], $srcTable ) );
2545 } else {
2546 $srcTable = $this->tableName( $srcTable );
2547 }
2548
2549 $sql = "INSERT $insertOptions INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
2550 " SELECT $startOpts " . implode( ',', $varMap ) .
2551 " FROM $srcTable $useIndex $ignoreIndex ";
2552
2553 if ( $conds != '*' ) {
2554 if ( is_array( $conds ) ) {
2555 $conds = $this->makeList( $conds, LIST_AND );
2556 }
2557 $sql .= " WHERE $conds";
2558 }
2559
2560 $sql .= " $tailOpts";
2561
2562 return $this->query( $sql, $fname );
2563 }
2564
2565 /**
2566 * Construct a LIMIT query with optional offset. This is used for query
2567 * pages. The SQL should be adjusted so that only the first $limit rows
2568 * are returned. If $offset is provided as well, then the first $offset
2569 * rows should be discarded, and the next $limit rows should be returned.
2570 * If the result of the query is not ordered, then the rows to be returned
2571 * are theoretically arbitrary.
2572 *
2573 * $sql is expected to be a SELECT, if that makes a difference.
2574 *
2575 * The version provided by default works in MySQL and SQLite. It will very
2576 * likely need to be overridden for most other DBMSes.
2577 *
2578 * @param string $sql SQL query we will append the limit too
2579 * @param int $limit The SQL limit
2580 * @param int|bool $offset The SQL offset (default false)
2581 * @throws DBUnexpectedError
2582 * @return string
2583 */
2584 public function limitResult( $sql, $limit, $offset = false ) {
2585 if ( !is_numeric( $limit ) ) {
2586 throw new DBUnexpectedError( $this, "Invalid non-numeric limit passed to limitResult()\n" );
2587 }
2588
2589 return "$sql LIMIT "
2590 . ( ( is_numeric( $offset ) && $offset != 0 ) ? "{$offset}," : "" )
2591 . "{$limit} ";
2592 }
2593
2594 public function unionSupportsOrderAndLimit() {
2595 return true; // True for almost every DB supported
2596 }
2597
2598 public function unionQueries( $sqls, $all ) {
2599 $glue = $all ? ') UNION ALL (' : ') UNION (';
2600
2601 return '(' . implode( $glue, $sqls ) . ')';
2602 }
2603
2604 public function conditional( $cond, $trueVal, $falseVal ) {
2605 if ( is_array( $cond ) ) {
2606 $cond = $this->makeList( $cond, LIST_AND );
2607 }
2608
2609 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
2610 }
2611
2612 public function strreplace( $orig, $old, $new ) {
2613 return "REPLACE({$orig}, {$old}, {$new})";
2614 }
2615
2616 public function getServerUptime() {
2617 return 0;
2618 }
2619
2620 public function wasDeadlock() {
2621 return false;
2622 }
2623
2624 public function wasLockTimeout() {
2625 return false;
2626 }
2627
2628 public function wasErrorReissuable() {
2629 return false;
2630 }
2631
2632 public function wasReadOnlyError() {
2633 return false;
2634 }
2635
2636 /**
2637 * Determines if the given query error was a connection drop
2638 * STUB
2639 *
2640 * @param integer|string $errno
2641 * @return bool
2642 */
2643 public function wasConnectionError( $errno ) {
2644 return false;
2645 }
2646
2647 /**
2648 * Perform a deadlock-prone transaction.
2649 *
2650 * This function invokes a callback function to perform a set of write
2651 * queries. If a deadlock occurs during the processing, the transaction
2652 * will be rolled back and the callback function will be called again.
2653 *
2654 * Avoid using this method outside of Job or Maintenance classes.
2655 *
2656 * Usage:
2657 * $dbw->deadlockLoop( callback, ... );
2658 *
2659 * Extra arguments are passed through to the specified callback function.
2660 * This method requires that no transactions are already active to avoid
2661 * causing premature commits or exceptions.
2662 *
2663 * Returns whatever the callback function returned on its successful,
2664 * iteration, or false on error, for example if the retry limit was
2665 * reached.
2666 *
2667 * @return mixed
2668 * @throws DBUnexpectedError
2669 * @throws Exception
2670 */
2671 public function deadlockLoop() {
2672 $args = func_get_args();
2673 $function = array_shift( $args );
2674 $tries = self::DEADLOCK_TRIES;
2675
2676 $this->begin( __METHOD__ );
2677
2678 $retVal = null;
2679 /** @var Exception $e */
2680 $e = null;
2681 do {
2682 try {
2683 $retVal = call_user_func_array( $function, $args );
2684 break;
2685 } catch ( DBQueryError $e ) {
2686 if ( $this->wasDeadlock() ) {
2687 // Retry after a randomized delay
2688 usleep( mt_rand( self::DEADLOCK_DELAY_MIN, self::DEADLOCK_DELAY_MAX ) );
2689 } else {
2690 // Throw the error back up
2691 throw $e;
2692 }
2693 }
2694 } while ( --$tries > 0 );
2695
2696 if ( $tries <= 0 ) {
2697 // Too many deadlocks; give up
2698 $this->rollback( __METHOD__ );
2699 throw $e;
2700 } else {
2701 $this->commit( __METHOD__ );
2702
2703 return $retVal;
2704 }
2705 }
2706
2707 public function masterPosWait( DBMasterPos $pos, $timeout ) {
2708 # Real waits are implemented in the subclass.
2709 return 0;
2710 }
2711
2712 public function getSlavePos() {
2713 # Stub
2714 return false;
2715 }
2716
2717 public function getMasterPos() {
2718 # Stub
2719 return false;
2720 }
2721
2722 public function serverIsReadOnly() {
2723 return false;
2724 }
2725
2726 final public function onTransactionResolution( callable $callback ) {
2727 if ( !$this->mTrxLevel ) {
2728 throw new DBUnexpectedError( $this, "No transaction is active." );
2729 }
2730 $this->mTrxEndCallbacks[] = [ $callback, wfGetCaller() ];
2731 }
2732
2733 final public function onTransactionIdle( callable $callback ) {
2734 $this->mTrxIdleCallbacks[] = [ $callback, wfGetCaller() ];
2735 if ( !$this->mTrxLevel ) {
2736 $this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE );
2737 }
2738 }
2739
2740 final public function onTransactionPreCommitOrIdle( callable $callback ) {
2741 if ( $this->mTrxLevel ) {
2742 $this->mTrxPreCommitCallbacks[] = [ $callback, wfGetCaller() ];
2743 } else {
2744 // If no transaction is active, then make one for this callback
2745 $this->startAtomic( __METHOD__ );
2746 try {
2747 call_user_func( $callback );
2748 $this->endAtomic( __METHOD__ );
2749 } catch ( Exception $e ) {
2750 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
2751 throw $e;
2752 }
2753 }
2754 }
2755
2756 final public function setTransactionListener( $name, callable $callback = null ) {
2757 if ( $callback ) {
2758 $this->mTrxRecurringCallbacks[$name] = [ $callback, wfGetCaller() ];
2759 } else {
2760 unset( $this->mTrxRecurringCallbacks[$name] );
2761 }
2762 }
2763
2764 /**
2765 * Whether to disable running of post-COMMIT/ROLLBACK callbacks
2766 *
2767 * This method should not be used outside of Database/LoadBalancer
2768 *
2769 * @param bool $suppress
2770 * @since 1.28
2771 */
2772 final public function setTrxEndCallbackSuppression( $suppress ) {
2773 $this->mTrxEndCallbacksSuppressed = $suppress;
2774 }
2775
2776 /**
2777 * Actually run and consume any "on transaction idle/resolution" callbacks.
2778 *
2779 * This method should not be used outside of Database/LoadBalancer
2780 *
2781 * @param integer $trigger IDatabase::TRIGGER_* constant
2782 * @since 1.20
2783 * @throws Exception
2784 */
2785 public function runOnTransactionIdleCallbacks( $trigger ) {
2786 if ( $this->mTrxEndCallbacksSuppressed ) {
2787 return;
2788 }
2789
2790 $autoTrx = $this->getFlag( DBO_TRX ); // automatic begin() enabled?
2791 /** @var Exception $e */
2792 $e = null; // first exception
2793 do { // callbacks may add callbacks :)
2794 $callbacks = array_merge(
2795 $this->mTrxIdleCallbacks,
2796 $this->mTrxEndCallbacks // include "transaction resolution" callbacks
2797 );
2798 $this->mTrxIdleCallbacks = []; // consumed (and recursion guard)
2799 $this->mTrxEndCallbacks = []; // consumed (recursion guard)
2800 foreach ( $callbacks as $callback ) {
2801 try {
2802 list( $phpCallback ) = $callback;
2803 $this->clearFlag( DBO_TRX ); // make each query its own transaction
2804 call_user_func_array( $phpCallback, [ $trigger ] );
2805 if ( $autoTrx ) {
2806 $this->setFlag( DBO_TRX ); // restore automatic begin()
2807 } else {
2808 $this->clearFlag( DBO_TRX ); // restore auto-commit
2809 }
2810 } catch ( Exception $ex ) {
2811 call_user_func( $this->errorLogger, $ex );
2812 $e = $e ?: $ex;
2813 // Some callbacks may use startAtomic/endAtomic, so make sure
2814 // their transactions are ended so other callbacks don't fail
2815 if ( $this->trxLevel() ) {
2816 $this->rollback( __METHOD__, self::FLUSHING_INTERNAL );
2817 }
2818 }
2819 }
2820 } while ( count( $this->mTrxIdleCallbacks ) );
2821
2822 if ( $e instanceof Exception ) {
2823 throw $e; // re-throw any first exception
2824 }
2825 }
2826
2827 /**
2828 * Actually run and consume any "on transaction pre-commit" callbacks.
2829 *
2830 * This method should not be used outside of Database/LoadBalancer
2831 *
2832 * @since 1.22
2833 * @throws Exception
2834 */
2835 public function runOnTransactionPreCommitCallbacks() {
2836 $e = null; // first exception
2837 do { // callbacks may add callbacks :)
2838 $callbacks = $this->mTrxPreCommitCallbacks;
2839 $this->mTrxPreCommitCallbacks = []; // consumed (and recursion guard)
2840 foreach ( $callbacks as $callback ) {
2841 try {
2842 list( $phpCallback ) = $callback;
2843 call_user_func( $phpCallback );
2844 } catch ( Exception $ex ) {
2845 call_user_func( $this->errorLogger, $ex );
2846 $e = $e ?: $ex;
2847 }
2848 }
2849 } while ( count( $this->mTrxPreCommitCallbacks ) );
2850
2851 if ( $e instanceof Exception ) {
2852 throw $e; // re-throw any first exception
2853 }
2854 }
2855
2856 /**
2857 * Actually run any "transaction listener" callbacks.
2858 *
2859 * This method should not be used outside of Database/LoadBalancer
2860 *
2861 * @param integer $trigger IDatabase::TRIGGER_* constant
2862 * @throws Exception
2863 * @since 1.20
2864 */
2865 public function runTransactionListenerCallbacks( $trigger ) {
2866 if ( $this->mTrxEndCallbacksSuppressed ) {
2867 return;
2868 }
2869
2870 /** @var Exception $e */
2871 $e = null; // first exception
2872
2873 foreach ( $this->mTrxRecurringCallbacks as $callback ) {
2874 try {
2875 list( $phpCallback ) = $callback;
2876 $phpCallback( $trigger, $this );
2877 } catch ( Exception $ex ) {
2878 call_user_func( $this->errorLogger, $ex );
2879 $e = $e ?: $ex;
2880 }
2881 }
2882
2883 if ( $e instanceof Exception ) {
2884 throw $e; // re-throw any first exception
2885 }
2886 }
2887
2888 final public function startAtomic( $fname = __METHOD__ ) {
2889 if ( !$this->mTrxLevel ) {
2890 $this->begin( $fname, self::TRANSACTION_INTERNAL );
2891 // If DBO_TRX is set, a series of startAtomic/endAtomic pairs will result
2892 // in all changes being in one transaction to keep requests transactional.
2893 if ( !$this->getFlag( DBO_TRX ) ) {
2894 $this->mTrxAutomaticAtomic = true;
2895 }
2896 }
2897
2898 $this->mTrxAtomicLevels[] = $fname;
2899 }
2900
2901 final public function endAtomic( $fname = __METHOD__ ) {
2902 if ( !$this->mTrxLevel ) {
2903 throw new DBUnexpectedError( $this, "No atomic transaction is open (got $fname)." );
2904 }
2905 if ( !$this->mTrxAtomicLevels ||
2906 array_pop( $this->mTrxAtomicLevels ) !== $fname
2907 ) {
2908 throw new DBUnexpectedError( $this, "Invalid atomic section ended (got $fname)." );
2909 }
2910
2911 if ( !$this->mTrxAtomicLevels && $this->mTrxAutomaticAtomic ) {
2912 $this->commit( $fname, self::FLUSHING_INTERNAL );
2913 }
2914 }
2915
2916 final public function doAtomicSection( $fname, callable $callback ) {
2917 $this->startAtomic( $fname );
2918 try {
2919 $res = call_user_func_array( $callback, [ $this, $fname ] );
2920 } catch ( Exception $e ) {
2921 $this->rollback( $fname, self::FLUSHING_INTERNAL );
2922 throw $e;
2923 }
2924 $this->endAtomic( $fname );
2925
2926 return $res;
2927 }
2928
2929 final public function begin( $fname = __METHOD__, $mode = self::TRANSACTION_EXPLICIT ) {
2930 // Protect against mismatched atomic section, transaction nesting, and snapshot loss
2931 if ( $this->mTrxLevel ) {
2932 if ( $this->mTrxAtomicLevels ) {
2933 $levels = implode( ', ', $this->mTrxAtomicLevels );
2934 $msg = "$fname: Got explicit BEGIN while atomic section(s) $levels are open.";
2935 throw new DBUnexpectedError( $this, $msg );
2936 } elseif ( !$this->mTrxAutomatic ) {
2937 $msg = "$fname: Explicit transaction already active (from {$this->mTrxFname}).";
2938 throw new DBUnexpectedError( $this, $msg );
2939 } else {
2940 // @TODO: make this an exception at some point
2941 $msg = "$fname: Implicit transaction already active (from {$this->mTrxFname}).";
2942 $this->queryLogger->error( $msg );
2943 return; // join the main transaction set
2944 }
2945 } elseif ( $this->getFlag( DBO_TRX ) && $mode !== self::TRANSACTION_INTERNAL ) {
2946 // @TODO: make this an exception at some point
2947 $msg = "$fname: Implicit transaction expected (DBO_TRX set).";
2948 $this->queryLogger->error( $msg );
2949 return; // let any writes be in the main transaction
2950 }
2951
2952 // Avoid fatals if close() was called
2953 $this->assertOpen();
2954
2955 $this->doBegin( $fname );
2956 $this->mTrxTimestamp = microtime( true );
2957 $this->mTrxFname = $fname;
2958 $this->mTrxDoneWrites = false;
2959 $this->mTrxAutomatic = ( $mode === self::TRANSACTION_INTERNAL );
2960 $this->mTrxAutomaticAtomic = false;
2961 $this->mTrxAtomicLevels = [];
2962 $this->mTrxShortId = wfRandomString( 12 );
2963 $this->mTrxWriteDuration = 0.0;
2964 $this->mTrxWriteQueryCount = 0;
2965 $this->mTrxWriteAdjDuration = 0.0;
2966 $this->mTrxWriteAdjQueryCount = 0;
2967 $this->mTrxWriteCallers = [];
2968 // First SELECT after BEGIN will establish the snapshot in REPEATABLE-READ.
2969 // Get an estimate of the replica DB lag before then, treating estimate staleness
2970 // as lag itself just to be safe
2971 $status = $this->getApproximateLagStatus();
2972 $this->mTrxReplicaLag = $status['lag'] + ( microtime( true ) - $status['since'] );
2973 }
2974
2975 /**
2976 * Issues the BEGIN command to the database server.
2977 *
2978 * @see DatabaseBase::begin()
2979 * @param string $fname
2980 */
2981 protected function doBegin( $fname ) {
2982 $this->query( 'BEGIN', $fname );
2983 $this->mTrxLevel = 1;
2984 }
2985
2986 final public function commit( $fname = __METHOD__, $flush = '' ) {
2987 if ( $this->mTrxLevel && $this->mTrxAtomicLevels ) {
2988 // There are still atomic sections open. This cannot be ignored
2989 $levels = implode( ', ', $this->mTrxAtomicLevels );
2990 throw new DBUnexpectedError(
2991 $this,
2992 "$fname: Got COMMIT while atomic sections $levels are still open."
2993 );
2994 }
2995
2996 if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
2997 if ( !$this->mTrxLevel ) {
2998 return; // nothing to do
2999 } elseif ( !$this->mTrxAutomatic ) {
3000 throw new DBUnexpectedError(
3001 $this,
3002 "$fname: Flushing an explicit transaction, getting out of sync."
3003 );
3004 }
3005 } else {
3006 if ( !$this->mTrxLevel ) {
3007 $this->queryLogger->error( "$fname: No transaction to commit, something got out of sync." );
3008 return; // nothing to do
3009 } elseif ( $this->mTrxAutomatic ) {
3010 // @TODO: make this an exception at some point
3011 $msg = "$fname: Explicit commit of implicit transaction.";
3012 $this->queryLogger->error( $msg );
3013 return; // wait for the main transaction set commit round
3014 }
3015 }
3016
3017 // Avoid fatals if close() was called
3018 $this->assertOpen();
3019
3020 $this->runOnTransactionPreCommitCallbacks();
3021 $writeTime = $this->pendingWriteQueryDuration( self::ESTIMATE_DB_APPLY );
3022 $this->doCommit( $fname );
3023 if ( $this->mTrxDoneWrites ) {
3024 $this->mDoneWrites = microtime( true );
3025 $this->trxProfiler->transactionWritingOut(
3026 $this->mServer, $this->mDBname, $this->mTrxShortId, $writeTime );
3027 }
3028
3029 $this->runOnTransactionIdleCallbacks( self::TRIGGER_COMMIT );
3030 $this->runTransactionListenerCallbacks( self::TRIGGER_COMMIT );
3031 }
3032
3033 /**
3034 * Issues the COMMIT command to the database server.
3035 *
3036 * @see DatabaseBase::commit()
3037 * @param string $fname
3038 */
3039 protected function doCommit( $fname ) {
3040 if ( $this->mTrxLevel ) {
3041 $this->query( 'COMMIT', $fname );
3042 $this->mTrxLevel = 0;
3043 }
3044 }
3045
3046 final public function rollback( $fname = __METHOD__, $flush = '' ) {
3047 if ( $flush === self::FLUSHING_INTERNAL || $flush === self::FLUSHING_ALL_PEERS ) {
3048 if ( !$this->mTrxLevel ) {
3049 return; // nothing to do
3050 }
3051 } else {
3052 if ( !$this->mTrxLevel ) {
3053 $this->queryLogger->error(
3054 "$fname: No transaction to rollback, something got out of sync." );
3055 return; // nothing to do
3056 } elseif ( $this->getFlag( DBO_TRX ) ) {
3057 throw new DBUnexpectedError(
3058 $this,
3059 "$fname: Expected mass rollback of all peer databases (DBO_TRX set)."
3060 );
3061 }
3062 }
3063
3064 // Avoid fatals if close() was called
3065 $this->assertOpen();
3066
3067 $this->doRollback( $fname );
3068 $this->mTrxAtomicLevels = [];
3069 if ( $this->mTrxDoneWrites ) {
3070 $this->trxProfiler->transactionWritingOut(
3071 $this->mServer, $this->mDBname, $this->mTrxShortId );
3072 }
3073
3074 $this->mTrxIdleCallbacks = []; // clear
3075 $this->mTrxPreCommitCallbacks = []; // clear
3076 $this->runOnTransactionIdleCallbacks( self::TRIGGER_ROLLBACK );
3077 $this->runTransactionListenerCallbacks( self::TRIGGER_ROLLBACK );
3078 }
3079
3080 /**
3081 * Issues the ROLLBACK command to the database server.
3082 *
3083 * @see DatabaseBase::rollback()
3084 * @param string $fname
3085 */
3086 protected function doRollback( $fname ) {
3087 if ( $this->mTrxLevel ) {
3088 # Disconnects cause rollback anyway, so ignore those errors
3089 $ignoreErrors = true;
3090 $this->query( 'ROLLBACK', $fname, $ignoreErrors );
3091 $this->mTrxLevel = 0;
3092 }
3093 }
3094
3095 public function flushSnapshot( $fname = __METHOD__ ) {
3096 if ( $this->writesOrCallbacksPending() || $this->explicitTrxActive() ) {
3097 // This only flushes transactions to clear snapshots, not to write data
3098 throw new DBUnexpectedError(
3099 $this,
3100 "$fname: Cannot COMMIT to clear snapshot because writes are pending."
3101 );
3102 }
3103
3104 $this->commit( $fname, self::FLUSHING_INTERNAL );
3105 }
3106
3107 public function explicitTrxActive() {
3108 return $this->mTrxLevel && ( $this->mTrxAtomicLevels || !$this->mTrxAutomatic );
3109 }
3110
3111 /**
3112 * Creates a new table with structure copied from existing table
3113 * Note that unlike most database abstraction functions, this function does not
3114 * automatically append database prefix, because it works at a lower
3115 * abstraction level.
3116 * The table names passed to this function shall not be quoted (this
3117 * function calls addIdentifierQuotes when needed).
3118 *
3119 * @param string $oldName Name of table whose structure should be copied
3120 * @param string $newName Name of table to be created
3121 * @param bool $temporary Whether the new table should be temporary
3122 * @param string $fname Calling function name
3123 * @throws RuntimeException
3124 * @return bool True if operation was successful
3125 */
3126 public function duplicateTableStructure( $oldName, $newName, $temporary = false,
3127 $fname = __METHOD__
3128 ) {
3129 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
3130 }
3131
3132 function listTables( $prefix = null, $fname = __METHOD__ ) {
3133 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
3134 }
3135
3136 /**
3137 * Reset the views process cache set by listViews()
3138 * @since 1.22
3139 */
3140 final public function clearViewsCache() {
3141 $this->allViews = null;
3142 }
3143
3144 /**
3145 * Lists all the VIEWs in the database
3146 *
3147 * For caching purposes the list of all views should be stored in
3148 * $this->allViews. The process cache can be cleared with clearViewsCache()
3149 *
3150 * @param string $prefix Only show VIEWs with this prefix, eg. unit_test_
3151 * @param string $fname Name of calling function
3152 * @throws RuntimeException
3153 * @return array
3154 * @since 1.22
3155 */
3156 public function listViews( $prefix = null, $fname = __METHOD__ ) {
3157 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
3158 }
3159
3160 /**
3161 * Differentiates between a TABLE and a VIEW
3162 *
3163 * @param string $name Name of the database-structure to test.
3164 * @throws RuntimeException
3165 * @return bool
3166 * @since 1.22
3167 */
3168 public function isView( $name ) {
3169 throw new RuntimeException( __METHOD__ . ' is not implemented in descendant class' );
3170 }
3171
3172 public function timestamp( $ts = 0 ) {
3173 return wfTimestamp( TS_MW, $ts );
3174 }
3175
3176 public function timestampOrNull( $ts = null ) {
3177 if ( is_null( $ts ) ) {
3178 return null;
3179 } else {
3180 return $this->timestamp( $ts );
3181 }
3182 }
3183
3184 /**
3185 * Take the result from a query, and wrap it in a ResultWrapper if
3186 * necessary. Boolean values are passed through as is, to indicate success
3187 * of write queries or failure.
3188 *
3189 * Once upon a time, DatabaseBase::query() returned a bare MySQL result
3190 * resource, and it was necessary to call this function to convert it to
3191 * a wrapper. Nowadays, raw database objects are never exposed to external
3192 * callers, so this is unnecessary in external code.
3193 *
3194 * @param bool|ResultWrapper|resource|object $result
3195 * @return bool|ResultWrapper
3196 */
3197 protected function resultObject( $result ) {
3198 if ( !$result ) {
3199 return false;
3200 } elseif ( $result instanceof ResultWrapper ) {
3201 return $result;
3202 } elseif ( $result === true ) {
3203 // Successful write query
3204 return $result;
3205 } else {
3206 return new ResultWrapper( $this, $result );
3207 }
3208 }
3209
3210 public function ping( &$rtt = null ) {
3211 // Avoid hitting the server if it was hit recently
3212 if ( $this->isOpen() && ( microtime( true ) - $this->lastPing ) < self::PING_TTL ) {
3213 if ( !func_num_args() || $this->mRTTEstimate > 0 ) {
3214 $rtt = $this->mRTTEstimate;
3215 return true; // don't care about $rtt
3216 }
3217 }
3218
3219 // This will reconnect if possible or return false if not
3220 $this->clearFlag( DBO_TRX, self::REMEMBER_PRIOR );
3221 $ok = ( $this->query( self::PING_QUERY, __METHOD__, true ) !== false );
3222 $this->restoreFlags( self::RESTORE_PRIOR );
3223
3224 if ( $ok ) {
3225 $rtt = $this->mRTTEstimate;
3226 }
3227
3228 return $ok;
3229 }
3230
3231 /**
3232 * @return bool
3233 */
3234 protected function reconnect() {
3235 $this->closeConnection();
3236 $this->mOpened = false;
3237 $this->mConn = false;
3238 try {
3239 $this->open( $this->mServer, $this->mUser, $this->mPassword, $this->mDBname );
3240 $this->lastPing = microtime( true );
3241 $ok = true;
3242 } catch ( DBConnectionError $e ) {
3243 $ok = false;
3244 }
3245
3246 return $ok;
3247 }
3248
3249 public function getSessionLagStatus() {
3250 return $this->getTransactionLagStatus() ?: $this->getApproximateLagStatus();
3251 }
3252
3253 /**
3254 * Get the replica DB lag when the current transaction started
3255 *
3256 * This is useful when transactions might use snapshot isolation
3257 * (e.g. REPEATABLE-READ in innodb), so the "real" lag of that data
3258 * is this lag plus transaction duration. If they don't, it is still
3259 * safe to be pessimistic. This returns null if there is no transaction.
3260 *
3261 * @return array|null ('lag': seconds or false on error, 'since': UNIX timestamp of BEGIN)
3262 * @since 1.27
3263 */
3264 public function getTransactionLagStatus() {
3265 return $this->mTrxLevel
3266 ? [ 'lag' => $this->mTrxReplicaLag, 'since' => $this->trxTimestamp() ]
3267 : null;
3268 }
3269
3270 /**
3271 * Get a replica DB lag estimate for this server
3272 *
3273 * @return array ('lag': seconds or false on error, 'since': UNIX timestamp of estimate)
3274 * @since 1.27
3275 */
3276 public function getApproximateLagStatus() {
3277 return [
3278 'lag' => $this->getLBInfo( 'replica' ) ? $this->getLag() : 0,
3279 'since' => microtime( true )
3280 ];
3281 }
3282
3283 /**
3284 * Merge the result of getSessionLagStatus() for several DBs
3285 * using the most pessimistic values to estimate the lag of
3286 * any data derived from them in combination
3287 *
3288 * This is information is useful for caching modules
3289 *
3290 * @see WANObjectCache::set()
3291 * @see WANObjectCache::getWithSetCallback()
3292 *
3293 * @param IDatabase $db1
3294 * @param IDatabase ...
3295 * @return array Map of values:
3296 * - lag: highest lag of any of the DBs or false on error (e.g. replication stopped)
3297 * - since: oldest UNIX timestamp of any of the DB lag estimates
3298 * - pending: whether any of the DBs have uncommitted changes
3299 * @since 1.27
3300 */
3301 public static function getCacheSetOptions( IDatabase $db1 ) {
3302 $res = [ 'lag' => 0, 'since' => INF, 'pending' => false ];
3303 foreach ( func_get_args() as $db ) {
3304 /** @var IDatabase $db */
3305 $status = $db->getSessionLagStatus();
3306 if ( $status['lag'] === false ) {
3307 $res['lag'] = false;
3308 } elseif ( $res['lag'] !== false ) {
3309 $res['lag'] = max( $res['lag'], $status['lag'] );
3310 }
3311 $res['since'] = min( $res['since'], $status['since'] );
3312 $res['pending'] = $res['pending'] ?: $db->writesPending();
3313 }
3314
3315 return $res;
3316 }
3317
3318 public function getLag() {
3319 return 0;
3320 }
3321
3322 function maxListLen() {
3323 return 0;
3324 }
3325
3326 public function encodeBlob( $b ) {
3327 return $b;
3328 }
3329
3330 public function decodeBlob( $b ) {
3331 if ( $b instanceof Blob ) {
3332 $b = $b->fetch();
3333 }
3334 return $b;
3335 }
3336
3337 public function setSessionOptions( array $options ) {
3338 }
3339
3340 /**
3341 * Read and execute SQL commands from a file.
3342 *
3343 * Returns true on success, error string or exception on failure (depending
3344 * on object's error ignore settings).
3345 *
3346 * @param string $filename File name to open
3347 * @param bool|callable $lineCallback Optional function called before reading each line
3348 * @param bool|callable $resultCallback Optional function called for each MySQL result
3349 * @param bool|string $fname Calling function name or false if name should be
3350 * generated dynamically using $filename
3351 * @param bool|callable $inputCallback Optional function called for each
3352 * complete line sent
3353 * @return bool|string
3354 * @throws Exception
3355 */
3356 public function sourceFile(
3357 $filename, $lineCallback = false, $resultCallback = false, $fname = false, $inputCallback = false
3358 ) {
3359 MediaWiki\suppressWarnings();
3360 $fp = fopen( $filename, 'r' );
3361 MediaWiki\restoreWarnings();
3362
3363 if ( false === $fp ) {
3364 throw new RuntimeException( "Could not open \"{$filename}\".\n" );
3365 }
3366
3367 if ( !$fname ) {
3368 $fname = __METHOD__ . "( $filename )";
3369 }
3370
3371 try {
3372 $error = $this->sourceStream( $fp, $lineCallback, $resultCallback, $fname, $inputCallback );
3373 } catch ( Exception $e ) {
3374 fclose( $fp );
3375 throw $e;
3376 }
3377
3378 fclose( $fp );
3379
3380 return $error;
3381 }
3382
3383 /**
3384 * Get the full path of a patch file. Originally based on archive()
3385 * from updaters.inc. Keep in mind this always returns a patch, as
3386 * it fails back to MySQL if no DB-specific patch can be found
3387 *
3388 * @param string $patch The name of the patch, like patch-something.sql
3389 * @return string Full path to patch file
3390 */
3391 public function patchPath( $patch ) {
3392 global $IP;
3393
3394 $dbType = $this->getType();
3395 if ( file_exists( "$IP/maintenance/$dbType/archives/$patch" ) ) {
3396 return "$IP/maintenance/$dbType/archives/$patch";
3397 } else {
3398 return "$IP/maintenance/archives/$patch";
3399 }
3400 }
3401
3402 public function setSchemaVars( $vars ) {
3403 $this->mSchemaVars = $vars;
3404 }
3405
3406 /**
3407 * Read and execute commands from an open file handle.
3408 *
3409 * Returns true on success, error string or exception on failure (depending
3410 * on object's error ignore settings).
3411 *
3412 * @param resource $fp File handle
3413 * @param bool|callable $lineCallback Optional function called before reading each query
3414 * @param bool|callable $resultCallback Optional function called for each MySQL result
3415 * @param string $fname Calling function name
3416 * @param bool|callable $inputCallback Optional function called for each complete query sent
3417 * @return bool|string
3418 */
3419 public function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
3420 $fname = __METHOD__, $inputCallback = false
3421 ) {
3422 $cmd = '';
3423
3424 while ( !feof( $fp ) ) {
3425 if ( $lineCallback ) {
3426 call_user_func( $lineCallback );
3427 }
3428
3429 $line = trim( fgets( $fp ) );
3430
3431 if ( $line == '' ) {
3432 continue;
3433 }
3434
3435 if ( '-' == $line[0] && '-' == $line[1] ) {
3436 continue;
3437 }
3438
3439 if ( $cmd != '' ) {
3440 $cmd .= ' ';
3441 }
3442
3443 $done = $this->streamStatementEnd( $cmd, $line );
3444
3445 $cmd .= "$line\n";
3446
3447 if ( $done || feof( $fp ) ) {
3448 $cmd = $this->replaceVars( $cmd );
3449
3450 if ( ( $inputCallback && call_user_func( $inputCallback, $cmd ) ) || !$inputCallback ) {
3451 $res = $this->query( $cmd, $fname );
3452
3453 if ( $resultCallback ) {
3454 call_user_func( $resultCallback, $res, $this );
3455 }
3456
3457 if ( false === $res ) {
3458 $err = $this->lastError();
3459
3460 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
3461 }
3462 }
3463 $cmd = '';
3464 }
3465 }
3466
3467 return true;
3468 }
3469
3470 /**
3471 * Called by sourceStream() to check if we've reached a statement end
3472 *
3473 * @param string $sql SQL assembled so far
3474 * @param string $newLine New line about to be added to $sql
3475 * @return bool Whether $newLine contains end of the statement
3476 */
3477 public function streamStatementEnd( &$sql, &$newLine ) {
3478 if ( $this->delimiter ) {
3479 $prev = $newLine;
3480 $newLine = preg_replace( '/' . preg_quote( $this->delimiter, '/' ) . '$/', '', $newLine );
3481 if ( $newLine != $prev ) {
3482 return true;
3483 }
3484 }
3485
3486 return false;
3487 }
3488
3489 /**
3490 * Database independent variable replacement. Replaces a set of variables
3491 * in an SQL statement with their contents as given by $this->getSchemaVars().
3492 *
3493 * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables.
3494 *
3495 * - '{$var}' should be used for text and is passed through the database's
3496 * addQuotes method.
3497 * - `{$var}` should be used for identifiers (e.g. table and database names).
3498 * It is passed through the database's addIdentifierQuotes method which
3499 * can be overridden if the database uses something other than backticks.
3500 * - / *_* / or / *$wgDBprefix* / passes the name that follows through the
3501 * database's tableName method.
3502 * - / *i* / passes the name that follows through the database's indexName method.
3503 * - In all other cases, / *$var* / is left unencoded. Except for table options,
3504 * its use should be avoided. In 1.24 and older, string encoding was applied.
3505 *
3506 * @param string $ins SQL statement to replace variables in
3507 * @return string The new SQL statement with variables replaced
3508 */
3509 protected function replaceVars( $ins ) {
3510 $vars = $this->getSchemaVars();
3511 return preg_replace_callback(
3512 '!
3513 /\* (\$wgDBprefix|[_i]) \*/ (\w*) | # 1-2. tableName, indexName
3514 \'\{\$ (\w+) }\' | # 3. addQuotes
3515 `\{\$ (\w+) }` | # 4. addIdentifierQuotes
3516 /\*\$ (\w+) \*/ # 5. leave unencoded
3517 !x',
3518 function ( $m ) use ( $vars ) {
3519 // Note: Because of <https://bugs.php.net/bug.php?id=51881>,
3520 // check for both nonexistent keys *and* the empty string.
3521 if ( isset( $m[1] ) && $m[1] !== '' ) {
3522 if ( $m[1] === 'i' ) {
3523 return $this->indexName( $m[2] );
3524 } else {
3525 return $this->tableName( $m[2] );
3526 }
3527 } elseif ( isset( $m[3] ) && $m[3] !== '' && array_key_exists( $m[3], $vars ) ) {
3528 return $this->addQuotes( $vars[$m[3]] );
3529 } elseif ( isset( $m[4] ) && $m[4] !== '' && array_key_exists( $m[4], $vars ) ) {
3530 return $this->addIdentifierQuotes( $vars[$m[4]] );
3531 } elseif ( isset( $m[5] ) && $m[5] !== '' && array_key_exists( $m[5], $vars ) ) {
3532 return $vars[$m[5]];
3533 } else {
3534 return $m[0];
3535 }
3536 },
3537 $ins
3538 );
3539 }
3540
3541 /**
3542 * Get schema variables. If none have been set via setSchemaVars(), then
3543 * use some defaults from the current object.
3544 *
3545 * @return array
3546 */
3547 protected function getSchemaVars() {
3548 if ( $this->mSchemaVars ) {
3549 return $this->mSchemaVars;
3550 } else {
3551 return $this->getDefaultSchemaVars();
3552 }
3553 }
3554
3555 /**
3556 * Get schema variables to use if none have been set via setSchemaVars().
3557 *
3558 * Override this in derived classes to provide variables for tables.sql
3559 * and SQL patch files.
3560 *
3561 * @return array
3562 */
3563 protected function getDefaultSchemaVars() {
3564 return [];
3565 }
3566
3567 public function lockIsFree( $lockName, $method ) {
3568 return true;
3569 }
3570
3571 public function lock( $lockName, $method, $timeout = 5 ) {
3572 $this->mNamedLocksHeld[$lockName] = 1;
3573
3574 return true;
3575 }
3576
3577 public function unlock( $lockName, $method ) {
3578 unset( $this->mNamedLocksHeld[$lockName] );
3579
3580 return true;
3581 }
3582
3583 public function getScopedLockAndFlush( $lockKey, $fname, $timeout ) {
3584 if ( $this->writesOrCallbacksPending() ) {
3585 // This only flushes transactions to clear snapshots, not to write data
3586 throw new DBUnexpectedError(
3587 $this,
3588 "$fname: Cannot COMMIT to clear snapshot because writes are pending."
3589 );
3590 }
3591
3592 if ( !$this->lock( $lockKey, $fname, $timeout ) ) {
3593 return null;
3594 }
3595
3596 $unlocker = new ScopedCallback( function () use ( $lockKey, $fname ) {
3597 if ( $this->trxLevel() ) {
3598 // There is a good chance an exception was thrown, causing any early return
3599 // from the caller. Let any error handler get a chance to issue rollback().
3600 // If there isn't one, let the error bubble up and trigger server-side rollback.
3601 $this->onTransactionResolution( function () use ( $lockKey, $fname ) {
3602 $this->unlock( $lockKey, $fname );
3603 } );
3604 } else {
3605 $this->unlock( $lockKey, $fname );
3606 }
3607 } );
3608
3609 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
3610
3611 return $unlocker;
3612 }
3613
3614 public function namedLocksEnqueue() {
3615 return false;
3616 }
3617
3618 /**
3619 * Lock specific tables
3620 *
3621 * @param array $read Array of tables to lock for read access
3622 * @param array $write Array of tables to lock for write access
3623 * @param string $method Name of caller
3624 * @param bool $lowPriority Whether to indicate writes to be LOW PRIORITY
3625 * @return bool
3626 */
3627 public function lockTables( $read, $write, $method, $lowPriority = true ) {
3628 return true;
3629 }
3630
3631 /**
3632 * Unlock specific tables
3633 *
3634 * @param string $method The caller
3635 * @return bool
3636 */
3637 public function unlockTables( $method ) {
3638 return true;
3639 }
3640
3641 /**
3642 * Delete a table
3643 * @param string $tableName
3644 * @param string $fName
3645 * @return bool|ResultWrapper
3646 * @since 1.18
3647 */
3648 public function dropTable( $tableName, $fName = __METHOD__ ) {
3649 if ( !$this->tableExists( $tableName, $fName ) ) {
3650 return false;
3651 }
3652 $sql = "DROP TABLE " . $this->tableName( $tableName );
3653 if ( $this->cascadingDeletes() ) {
3654 $sql .= " CASCADE";
3655 }
3656
3657 return $this->query( $sql, $fName );
3658 }
3659
3660 /**
3661 * Get search engine class. All subclasses of this need to implement this
3662 * if they wish to use searching.
3663 *
3664 * @return string
3665 */
3666 public function getSearchEngine() {
3667 return 'SearchEngineDummy';
3668 }
3669
3670 public function getInfinity() {
3671 return 'infinity';
3672 }
3673
3674 public function encodeExpiry( $expiry ) {
3675 return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
3676 ? $this->getInfinity()
3677 : $this->timestamp( $expiry );
3678 }
3679
3680 public function decodeExpiry( $expiry, $format = TS_MW ) {
3681 return ( $expiry == '' || $expiry == 'infinity' || $expiry == $this->getInfinity() )
3682 ? 'infinity'
3683 : wfTimestamp( $format, $expiry );
3684 }
3685
3686 public function setBigSelects( $value = true ) {
3687 // no-op
3688 }
3689
3690 public function isReadOnly() {
3691 return ( $this->getReadOnlyReason() !== false );
3692 }
3693
3694 /**
3695 * @return string|bool Reason this DB is read-only or false if it is not
3696 */
3697 protected function getReadOnlyReason() {
3698 $reason = $this->getLBInfo( 'readOnlyReason' );
3699
3700 return is_string( $reason ) ? $reason : false;
3701 }
3702
3703 /**
3704 * @since 1.19
3705 * @return string
3706 */
3707 public function __toString() {
3708 return (string)$this->mConn;
3709 }
3710
3711 /**
3712 * Run a few simple sanity checks
3713 */
3714 public function __destruct() {
3715 if ( $this->mTrxLevel && $this->mTrxDoneWrites ) {
3716 trigger_error( "Uncommitted DB writes (transaction from {$this->mTrxFname})." );
3717 }
3718 $danglingCallbacks = array_merge(
3719 $this->mTrxIdleCallbacks,
3720 $this->mTrxPreCommitCallbacks,
3721 $this->mTrxEndCallbacks
3722 );
3723 if ( $danglingCallbacks ) {
3724 $callers = [];
3725 foreach ( $danglingCallbacks as $callbackInfo ) {
3726 $callers[] = $callbackInfo[1];
3727 }
3728 $callers = implode( ', ', $callers );
3729 trigger_error( "DB transaction callbacks still pending (from $callers)." );
3730 }
3731 }
3732 }
3733
3734 /**
3735 * @since 1.27
3736 */
3737 abstract class Database extends DatabaseBase {
3738 // B/C until nothing type hints for DatabaseBase
3739 // @TODO: finish renaming DatabaseBase => Database
3740 }